2017-08-07 1 views
0

私はおそらく20の異なる投稿を読んでいますが、私は何を探していますか?XAML:インデックスに列挙型を使用してオブジェクトをリストするためにIsCheckedをバインドします

私はいくつかのチェックボックスを使ってウィンドウを作っています。これらのチェックボックスでは、List内のオブジェクトのメンバー変数にIsChecked値をバインドする必要があります。

私はこのような何かしようとしている: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> 

のViewModel

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[InstallViewModel.MyFeatureEnum.FEATURE1].Selected, Mode=TwoWay}"> 

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

をのようなもので:私が欲しいもの

は交換することですか?

答えて

1

これは可能ですか?

List<Feature>を使用していますが、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}" /> 
+0

ハレルヤ!それは本当に簡単でした:D – jbudreau

関連する問題