2012-05-09 133 views
2

我有以下情况的对象的具体名单枚举:绑定单选按钮,在

  • 我有对象(List<MyObjects>)的列表。
  • 的Class MyObjects包含一个名为States枚举和其他一些属性,如ObjectName

    public enum States { State1, State2, State3, State4, State5 } 
    public string ObjectName { get; set; } 
    // some more Properties 
    

    和私人领域,像这样的属性:

    private States _state = States.State1; // State1 is the default state 
    
    public States State 
    { 
        get { return _state; } 
        set 
        { 
        if (_state != value) 
        { 
         _state = value; 
         OnPropertyChanged("State"); 
        } 
        } 
    } 
    
  • 在我的XAML我想在列表视图中显示MyObjects的列表,并为我的枚举的每个状态显示五个单选按钮。我这样做如下:

    <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> 
    
  • 我使用的是EnumToBoolean转换器,它看起来是这样的:

    [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; 
        } 
    } 
    

的绑定工作,单选按钮的作品,但问题的显示器是当我检查ListView中第一个元素的另一个单选按钮时, State被正确保存。当我现在更改ListView中选定的项目,然后再次选择ListView中的第一个项目时,没有选中单选按钮,因为State属性的获取器不会被调用。

我正在寻找一个解决方案,但我的具体问题是,我有一个MyObjects其中包含一个状态和更改选定的项目,选定的单选按钮也应该改变的列表。

我希望有人了解我的问题,并可以提供帮助。

由于提前, 迈克

+1

您可能对[此问题]感兴趣(http://stackoverflow.com/questions/9145606/),因为您的代码相当多。 –

+0

感谢您的提示。我用[this](http://stackoverflow.com/a/9145914/1384848)答案来解决我的问题。它工作得很好。 –

回答

0

您的代码有效,我已经在全新项目中重新创建了所有代码,并在更改SelectedItem时正确更新单选按钮以反映最新值(即将选择更改为非默认值后) 。

我认为你必须在其中干扰项目别的东西,你应该尝试取出溶液中片和隔离测试出来。

0

如果你使用MVVM,我找到了一个好办法用一个ICommand更新视图模型中的SelectedItem是。 XAML看起来像这样。

<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> 

视图模型会是这个样子:

ICommand UpdateSelectedItemCommand {get;set;} 

.. 

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

与实现的ICommand您的自定义对象替换DelegateCommand。