During the process of writing a WPF application recently, I had the need for a data bound list of items where the options had to be mutually exclusive, so I figured something like a list of RadioButtons would be in order.
However, when I started looking around I could not find a RadioButtonList or anything that fitted the bill out-of-the-box. Therefore, I thought I’d put something together myself; I also needed to have the list render horizontally rather than vertically. This is what I came up with:
I defined a Style and the ItemsPanelTemplate in the Resources property of my Window that contains all the necessary XAML for the effects I need; note that you could just as easily define this XAML inline on the ListBox as opposed to using the Resources property. I then applied these two new elements to an instance of ListBox using the following mark-up:
ItemsSource="{Binding MyDataList}"
SelectedValue="{Binding MyDataListSelectedValue}"
ItemContainerStyle="{StaticResource RadioButtonItemStyle}"
ItemsPanel="{StaticResource HorizontalItemsPanel}" />
I’ve highlighted where I’ve used the two resources.
For this example the data list is trivial and is provided by a ViewModel class attached to the DataContext of the Window; I’m only showing the code here as a example of how you can add sample data to an application, as well as proving that the above XAML all works as expected using a bound list of data:
public class MainWindowViewModel
{
public IEnumerable
{
get
{
yield return "Stan";
yield return "Cartman";
yield return "Kenny";
yield return "Karl";
}
}
public string MyDataListSelectedValue
{
get { return "Cartman"; }
set { /* TODO: save the value */ }
}
}
Here’s what it all looks like in the Visual Studio designer:
And there we have it, job done. Hope this helps someone, happy XAML hacking.