2

i have got the following situation:

  • I have a list of objects (List<MyObjects>).
  • The Class MyObjects contains an enum called States and some other Properties like ObjectName:

    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

M.E.
  • 2,759
  • 3
  • 24
  • 33
  • 1
    You might be interested in [this question](http://stackoverflow.com/questions/9145606/) as your code is rather redundant. – H.B. May 09 '12 at 15:34
  • Thanks for the hint. I used [this](http://stackoverflow.com/a/9145914/1384848) answer to solve my problem. It just worked fine. – M.E. May 10 '12 at 08:26

2 Answers2

0

If you're using MVVM, I have found a good approach to be using an ICommand to update the SelectedItem in the view model. The XAML would look like this.

    <RadioButton x:Name="state5RB"
    Content="{x:Static States.State5}"
    Command="{Binding UpdateSelectedItemCommand}"
    IsChecked="{Binding Path=State,
        UpdateSourceTrigger=PropertyChanged,
        Converter={StaticResource enumToBool},
        ConverterParameter={x:Static States.State5},
        Mode=TwoWay}">
       <RadioButton.CommandParameter>
           <myEnum:State>State5</myEnum:State>          
       </RadioButton.CommandParameter>

   </RadioButton>

The ViewModel would look something like this:

ICommand UpdateSelectedItemCommand {get;set;}

..

UpdateSelectedItemCommand = new DelegateCommand( (param) =>
{
   State= (States) param;
});

Replace DelegateCommand with your custom object that implements ICommand.

Brian
  • 6,910
  • 8
  • 44
  • 82
0

Your code works, I've re-created it all in a fresh project and it correctly updates the radio button to reflect the up to date value when you change the SelectedItem (i.e. after changing the selection to non-default).

I think you have something else in the project which is interfering and you should try taking out pieces of the solution and testing them out in isolation.

Slugart
  • 4,535
  • 24
  • 32