gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 16:32:00 26 4
gpt4 key购买 nike

我有以下情况:

  • 我有一个对象列表 ( 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com