2016-07-13 2 views
0

データソースMyInformationにバインドしているグリッドビューがあるとします。列の1つがチェックボックスです。私はそれに何かを縛りたい。 ViewModelには私のチェックボックスがメンバーと紐付けされていません

ItemsSource="{Binding MyInformation}" 

public ObservableCollection<Container> MyInformation 
    { 
     get 
     { 
      if (this.myInformation == null) 
      { 
       this.myInformation = new ObservableCollection<Container>(); 
      } 
      return this.myInformation; 
     } 
     set 
     { 
      if (this.myInformation != value) 
      { 
       this.myInformation = value; 
       this.OnPropertyChanged("MyInformation"); 
      } 
     } 
    } 

クラスコンテナには「GoodValue」というメンバーがあります。

public class Container 
{ 
    public bool GoodValue {get;set;} 
    // 
} 

私は、メンバーにチェックボックスをバインドしています。

<DataTemplate> 
    <CheckBox HorizontalAlignment="Center" IsChecked="{Binding GoodValue, Converter={StaticResource ShortToBooleanConverter}}" Click="CheckBox_Checked"></CheckBox> 
    </DataTemplate> 

私はGoodValueを考えると、私はViewModelにで作成したプロパティGoodValueを持っていないが、コンテナのメンバーです。 ObservableCollectionにはそれが自動的に含まれます。

問題は、データベースからデータを読み取るたびに発生します。チェックボックスはオフになっています。だから私は自分のコードを疑う。ヒントありがとう。

答えて

0

あなたは2つのことを行うことができます。

  1. チェックをいくつかバインドエラー
  2. があなたのクラスContainerにインターフェイスをINotifyPropertyChangedの実装がある場合。

    パブリッククラスContainer: {

    private bool _goodValue; 
    
        public string GoodValue 
        { 
         get 
         { 
          return _goodValue; 
         } 
         set 
         { 
          _goodValue = value; 
          OnPropertyChanged("GoodValue"); 
         } 
        } 
    
        protected void OnPropertyChanged(string name) 
        { 
         PropertyChangedEventHandler handler = PropertyChanged; 
         if (PropertyChanged != null) 
          PropertyChanged(this, new PropertyChangedEventArgs(name)); 
        } 
    
        public event PropertyChangedEventHandler PropertyChanged; 
    } 
    

をINotifyPropertyChangedの新しいアイテムがコレクションから挿入または削除されたときに、あなたのビューに通知したい場合はのObservableCollectionは便利ですが、もしそのオブジェクトに含まれるオブジェクトがInotifyPropertyChangedを実装していない場合、そのオブジェクトのプロパティの変更はビューの変更に影響しません。

+1

エラーを見つけます。私のShortToBooleanConverterが間違っています。 trueの場合は1、falseの場合は0です。しかし、私の場合、テーブルの値は1/0ではありません。私はコンバータを書き直さなければならない。 – Bigeyes

関連する問題