XAML:绑定IsChecked以使用枚举来列出对象

时间:2017-08-07 23:24:20

标签: c# wpf xaml

我已经阅读了大约20个不同的帖子,仍然可以将我想要做的事情放在一起(也许我会以错误的方式去做?)。

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

我正在尝试这样的事情:

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

这可能吗?

1 个答案:

答案 0 :(得分:1)

  

这可能吗?

不使用funcattrmeth = [] funcattrmeth = dir(numpy) len(funcattrmeth) for i in funcattrmeth: j = 'numpy.' + i k = type(j) print ("subentity " + j + " of type " + k ) 但您可以使用subentity numpy.var of type function subentity numpy.vdot of type builtin_function_or_method subentity numpy.vectorize of type type subentity numpy.version of type module subentity numpy.void of type type

List<Feature>
Dictionary<MyFeatureEnum, Feature>