i have got the following situation:
- I have a list of objects (
List<MyObjects>
). The Class
MyObjects
contains an enum calledStates
and some other Properties likeObjectName
:public enum States { State1, State2, State3, State4, State5 } public string ObjectName { get; set; } // some more Properties
and a private field and a property like this:
private States _state = States.State1; // State1 is the default state public States State { get { return _state; } set { if (_state != value) { _state = value; OnPropertyChanged("State"); } } }
In my XAML I want to display the list of
MyObjects
in a ListView and five Radio Buttons for each state of my enum. I do this as follows:<ListView x:Name="myObjectsListView" ItemsSource="{Binding MyObjectList}"> <ListView.View> <GridView> <GridViewColumn DisplayMemberBinding="{Binding ObjectName}" Header="Object Name" Width="Auto"/> <!-- some more GridViewColumns --> </GridView> </ListView.View> </ListView> <StackPanel> <RadioButton x:Name="state1RB" Content="{x:Static States.State1}" IsChecked="{Binding ElementName=myObjectsListView, Path=SelectedItem.State, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource enumToBool}, ConverterParameter={x:Static States.State1}, Mode=TwoWay}"/> <RadioButton x:Name="state2RB" Content="{x:Static States.State2}" IsChecked="{Binding ElementName=myObjectsListView, Path=SelectedItem.State, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource enumToBool}, ConverterParameter={x:Static States.State2}, Mode=TwoWay}"/> <RadioButton x:Name="state3RB" Content="{x:Static States.State3}" IsChecked="{Binding ElementName=myObjectsListView, Path=SelectedItem.State, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource enumToBool}, ConverterParameter={x:Static States.State3}, Mode=TwoWay}"/> <RadioButton x:Name="state4RB" Content="{x:Static States.State4}" IsChecked="{Binding ElementName=myObjectsListView, Path=SelectedItem.State, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource enumToBool}, ConverterParameter={x:Static States.State4}, Mode=TwoWay}"/> <RadioButton x:Name="state5RB" Content="{x:Static States.State5}" IsChecked="{Binding ElementName=myObjectsListView, Path=SelectedItem.State, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource enumToBool}, ConverterParameter={x:Static States.State5}, Mode=TwoWay}"/> </StackPanel>
I'm using an EnumToBoolean Converter which looks like this:
[ValueConversion(typeof(System.Enum), typeof(bool))] public class EnumToBooleanConverter : IValueConverter { public object Convert (object value, Type targetType, object parameter, CultureInfo culture) { return value.Equals (parameter); } public object ConvertBack (object value, Type targetType, object parameter, CultureInfo culture) { if ((Boolean)value) return parameter; return null; } }
The binding works, the display of the radio button works but the problem is when i check another Radio Button for the first element in the ListView, the
State
is saved correctly. When i now change the selected item in the ListView and then select the first item in the ListView again, no Radio Button is checked, because the getter of the State
property doesn't get called.
I was searching for a solution but my specific problem is, that i have a list of MyObjects
which contains a state and on changing the selected item, the selected radio button should also be changed.
I hope someone understand my problem and can help.
Thanks in advance, Mike