I have a ItemsControl
in my program that contains a list of radio buttons.
<ItemsControl ItemsSource="{Binding Insertions}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<RadioButton GroupName="Insertions"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
How do I find the selected radio button in the group Insertions
in a MVVM manner?
Most of the examples I have found on the internet involve setting individual boolean properties that you bind the IsChecked
property to with the help of a converter.
Is there an equivalent of the ListBox
SelectedItem
that I can bind to?
One solution that comes to mind is to add an
IsChecked
boolean property to your Insertion entities and bind that to the `IsChecked' property of the Radio button. This way you can check the 'Checked' radio button in View Model.Here is a quick and dirty example.
NB: I ignored the fact that the IsChecked can also be
null
, you could handle that usingbool?
if required.The simple ViewModel
The XAML - The code behind is not shown since it has no code other than than the generated code.