I was working on a simple voting control in silverlight and ran into a problem that I couldn't figure out how to solve. Coming from an ASP.NET background, I keep wanting to learn silverlight more and more and see how it can fit into the business world. For those of you that read this blog you know the few things I have done in Silverlight have to do with multimedia. Anyway... so here was my problem
I wanted to group RadioButtons in a ListBox. Simple enough right? Well this ListBox was going to use binding and not be static with items in it. It was reading data from an XML file and using a DataTemplate to create however many RadioButtons I needed in the ListBox. Below is the code I was using.
--------------------------------------
XAML:
<ListBox x:Name="listVotingOptions">
<ListBox.ItemTemplate>
<DataTemplate>
<RadioButton Content="{Binding Option}" GroupName="votingGroup" Tag="{Binding ID}"></RadioButton>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Code Behind:
//....code here for var results = blah blah blah
listVotingOptions.ItemsSource = results.ToList();
--------------------------------------
Looks like it would work. However, it doesn't. Here was best I found on it searching the Internet.
http://stackoverflow.com/questions/170061/is-there-a-way-to-group-radiobuttons-generated-from-the-itemtemplate-of-an-itemsc
Now I understand I'm not a Silverlight expert, but it has to do something with the logical tree as explained in the link above. I took some time and used the VisualTreeHelper and noticed that the listbox did not have anything that contained RadioButtons.
If any of you can shed some more light onto why this is that would be greatly appreciated :)
The link above also contains a link with someone who got it to work via Behaviors.
Note: This is a great page for learning how to use a listbox in silverlight:
http://blogs.msdn.com/delay/archive/2008/03/05/lb-sv-faq-examples-notes-tips-and-more-for-silverlight-2-beta-1-s-listbox-and-scrollviewer-controls.aspx
1806b4a8-d8cf-4228-81ad-22c00c4ff11e|0|.0
Silverlight
silverlight