【问题标题】:Bind radio buttons to enum in a specific list of objects将单选按钮绑定到特定对象列表中的枚举
【发布时间】:2012-05-18 02:58:20
【问题描述】:

我遇到了以下情况:

  • 我有一个对象列表 (List<MyObjects>)。
  • 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 中,我想在 ListView 中显示 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 属性的 getter 没有被调用。

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

我希望有人能理解我的问题并能提供帮助。

提前致谢, 迈克

【问题讨论】:

  • 您可能对this question 感兴趣,因为您的代码相当多余。
  • 感谢您的提示。我用this 回答来解决我的问题。它工作得很好。

标签: c# wpf enums radio-button


【解决方案1】:

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

我认为您的项目中存在其他干扰因素,您应该尝试取出解决方案的各个部分并单独测试它们。

【讨论】:

    【解决方案2】:

    如果您使用的是 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>
    

    ViewModel 看起来像这样:

    ICommand UpdateSelectedItemCommand {get;set;}
    
    ..
    
    UpdateSelectedItemCommand = new DelegateCommand( (param) =>
    {
       State= (States) param;
    });
    

    将 DelegateCommand 替换为实现 ICommand 的自定义对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-29
      • 1970-01-01
      • 2014-10-14
      • 2011-05-06
      • 2013-08-19
      • 1970-01-01
      • 2014-06-06
      • 1970-01-01
      相关资源
      最近更新 更多