c# - 将单选按钮绑定(bind)到特定对象列表中的枚举

标签 c# wpf enums radio-button

我有以下情况:

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

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

    还有一个私有(private)字段和一个像这样的属性:

    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 中,我的枚举的每个状态都有五个单选按钮。我这样做如下:

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

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

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

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

提前致谢, 迈克

最佳答案

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

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

关于c# - 将单选按钮绑定(bind)到特定对象列表中的枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10518724/

相关文章:

c# - 我如何从框架中获取页面实例?

kotlin - 如何强制立即实例化枚举值?

C# 生成随机 int - 神秘

c# - 流是否在 try/catch 语句中自动处理?

WPF 绑定(bind)表达式和依赖属性

c# - 无法让 WPF ItemsControl 垂直拉伸(stretch)

c# - AffectsMeasure 或 AffectsArrange

c# - 在具有多个对应名称的枚举值上调用 ToString() 时,什么决定选择哪个名称?

c# - 枚举加法与减法和类型转换

c# - 使用 Entity Framework 6 返回具有最大列值的记录