XAML:使用枚举将 IsChecked 绑定到列表对象以进行索引 [英] XAML: Bind IsChecked to list object using an enum to index

查看:32
本文介绍了XAML:使用枚举将 IsChecked 绑定到列表对象以进行索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了大约 20 篇不同的帖子,但仍然可以将我想要做的事情放在一起(也许我的方法不对?).

I've read through probably 20 different posts and still can put together what I'm looking to do (maybe I'm going about it the wrong way?).

我想制作一个带有几个复选框的窗口.这些复选框需要将其 IsChecked 值绑定到 List 中对象的成员变量.

I want to make a window with a few checkboxes. These checkboxes need to have their IsChecked value bound to a member variable of an object in a List.

我正在尝试这样的事情:

I'm trying something like this:

XAML

<CheckBox IsChecked="{Binding Features[0].Selected, Mode=TwoWay}">
    <TextBlock Text="Feature 1" />
</CheckBox>
<CheckBox IsChecked="{Binding Features[1].Selected, Mode=TwoWay}">
    <TextBlock Text="Feature 2" />
</CheckBox>

视图模型

  public static enum MyFeatureEnum
  {
     FEATURE1,
     FEATURE2,
     FEATURE3,
     ...
  }

  public class Feature : INotifyPropertyChanged
  {
     private bool _supported;
     private bool _visible;
     private bool _selected;
     public MyFeatureEnum FeatureEnum { get; set; }

     public bool Supported
     {
        get { return _supported; }
        set { _supported = value; NotifyPropertyChanged("Supported"); }
     }
     public bool Visible
     {
        get { return _visible; }
        set { _visible = value; NotifyPropertyChanged("Visible"); }
     }
     public bool Selected
     {
        get { return _selected; }
        set { _selected = value; NotifyPropertyChanged("Selected"); }
     }

     public Feature(MyFeatureEnum featureEnum)
     {
        _supported = false;
        _visible = false;
        _selected = false;
        FeatureEnum = featureEnum;
     }

     public event PropertyChangedEventHandler PropertyChanged;
     private void NotifyPropertyChanged(string propertyName)
     {
        if (PropertyChanged != null)
        {
           PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
     }
  }

  public List<Feature> Features { get; set; }

  public InstallViewModel()
  {
     ...
     // Generate the list of features
     Features = new List<Feature>();
     foreach (MyFeatureEnum f in Enum.GetValues(typeof(MyFeatureEnum)))
     {
        Features.Add(new Feature(f));
     }
     ...
  }

这有效.

我想要的是替换:

<CheckBox IsChecked="{Binding Features[0].Selected, Mode=TwoWay}">

类似于:

<CheckBox IsChecked="{Binding Features[InstallViewModel.MyFeatureEnum.FEATURE1].Selected, Mode=TwoWay}">

这可能吗?

推荐答案

这可能吗?

不使用 List 但你可以使用 Dictionary:

Not using a List<Feature> but you could use a Dictionary<MyFeatureEnum, Feature>:

public class InstallViewModel
{
    public Dictionary<MyFeatureEnum, Feature> Features { get; set; }

    public InstallViewModel()
    {
        // Generate the list of features
        Features = new Dictionary<MyFeatureEnum, Feature>();
        foreach (MyFeatureEnum f in Enum.GetValues(typeof(MyFeatureEnum)))
        {
            Features.Add(f, new Feature(f));
        }
    }
}

<小时>

<CheckBox IsChecked="{Binding Features[FEATURE1].Selected, Mode=TwoWay}" />

这篇关于XAML:使用枚举将 IsChecked 绑定到列表对象以进行索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆