2011-05-27 6 views
2

私の問題は「スコープ」と思われますが、それは正しい用語であるとは確信していません。私は、カスタムオブジェクト内のプロパティが設定されているときに、再評価するための読み取り専用リストに通知したいと思います。私はそれが単にその存在を認識していないと信じています。たぶん私は考えることができないこの周りの簡単な方法がありますが、私は空白を描いています。クラス内のクラスのプロパティでINotifyPropertyChangedを使用する方法..?

私はこれを言葉にするのが難しいと思っています。だから、ここで簡単なコードを書いています。

private CvarAspectRatios _aspectRatio = new CvarAspectRatios("none", GetRatio()); 
public CvarAspectRatios AspectRatio 
{ 
    get { return _aspectRatio; } 
    set 
    {             // This setter never gets hit since I bind to this 
     if (value != null)       // object's 'Value' property now. 
     { 
      _aspectRatio = value; 
      NotifyPropertyChanged("AspectRatio"); 
      NotifyPropertyChanged("ResolutionList"); // I want to inform ResolutionList 
     }            // that it needs to repopulate based 
    }             // on this property: AspectRatio 
} 

private ResolutionCollection _resolutionList = ResolutionCollection.GetResolutionCollection(); 
public ResolutionCollection ResolutionList 
{ 
    get 
    { 
     ResolutionCollection list = new ResolutionCollection(); 
     if (AspectRatio != null && AspectRatio.Value != null) 
     { 
      foreach (Resolutions res in _resolutionList.Where(i => i.Compatibility == AspectRatio.Value.Compatibility)) 
      { 
       list.Add(res); 
      } 
      return list; 
     } 
     return _resolutionList; 
    } 
} 

CvarAspectRatiosクラス::オブジェクト内

プロパティとは、私はにデータバインディングしています

public class CVarAspectRatios : INotifyPropertyChanged 
{ 
    private string _defaultValue; 
    public string DefaultValue 
    { 
     get { return _defaultValue; } 
     set { _defaultValue = value; NotifyPropertyChanged("DefaultValue"); } 
    } 

    private AspectRatios _value; 
    public AspectRatios Value 
    { 
     get { return _value; } 
     set 
     { 
      _value = value; 
      NotifyPropertyChanged("Value"); 
      NotifyPropertyChanged("ResolutionList"); // This value gets set, and I'd like for ResolutionList to update 
     }            // but it cannot find ResolutionList. No errors or anything. Just 
    }             // no update. 

    public AspectRatios() { } 

    public AspectRatios(string defaultValue, AspectRatios val) 
    { 
     DefaultValue = defaultValue; 
     Value = val; 
    } 

    // Implementation of INotifyPropertyChanged snipped out here 
} 

あなたの人々はどう思いますか?サンプルアプリケーションをご希望の場合は、私は1つをアップすることができます。

答えて

3

CVarAspectRatiosはINotifyPropertyChangedを実装しているため、ViewModelクラスをAspectRatioのPropertyChangedイベントにサブスクライブすることができます。

public class YourViewModel 
{ 
    public YourViewModel() 
    { 
     AspectRatio.PropertyChanged += AspectRatio_PropertyChanged; 
    } 

    void AspectRatio_PropertyChanged(object sender, PropertyChangedEventArgs e) 
    { 
     if (e.PropertyName == "Value") 
      NotifyPropertyChanged("ResolutionList"); 
    }   
} 

ちょうどあなたがアスペクト比のオブジェクト(オブジェクト参照の変更ではなく、そのオブジェクトの値だけプロパティの場合)ことを捨てた場合、あなたは捨て1のイベントから退会する必要があることに注意してください。ただ動作するはずです何かに既存のコードを変換するために

+0

Sir、それはすばらしくシンプルで、うまくいく。ありがとう! – erodewald

+0

DataGridViewにバインドされているときにこれを行うと、InvalidOperationExceptionが発生します。 'BindingSourceを独自のデータソースにすることはできません。 DataSourceプロパティとDataMemberプロパティをBindingSourceを参照する値に設定しないでください。 ' – mphair

+0

何も設定されていないため、上記の実装によってエラーが発生するかどうかはわかりません。 – Adrian

0

変更されたプロパティに基づいてリストを更新する必要がある場合、リスト(またはより良いカプセル化のためのリストマネージャオブジェクト)は通常、プロパティをホストするオブジェクトのPropertyChangedイベントにサブスクライブする必要があります。リストがそれ自体が同じオブジェクトのプロパティである場合、この場合のように、プロパティのセッタがリストを更新するメソッドを呼び出すのがより簡単で希薄です。

1

ResolutionListをセパレータAspectRatiosから呼び出された別のプライベートメソッドに再投入するのを除外しないのはなぜですか?

+0

受け入れ答えよりも少し複雑にかかわらず、私は、これがうまくいくと信じています。 – erodewald

2

private CvarAspectRatios _aspectRatio; //No field initialization because that would not attach event handler, you could do it though and take care of the handler alone in the ctor 
public CvarAspectRatios AspectRatio 
{ 
    get { return _aspectRatio; } 
    set 
    { 
     if (_aspectRatio != value) // WTH @ "value != null" 
     { 
      _aspectRatio.PropertyChanged -= AspectRatio_PropertyChanged; 
      _aspectRatio = value; 
      _aspectRatio.PropertyChanged += new PropertyChangedEventHandler(AspectRatio_PropertyChanged); 
      NotifyPropertyChanged("AspectRatio"); 
     } 
    } 
} 

void AspectRatio_PropertyChanged(object sender, PropertyChangedEventArgs e) 
{ 
    if (e.PropertyName == "Value") 
    { 
     NotifyPropertyChanged("ResolutionList"); 
    } 
} 
+0

申し訳ありませんが、私はそれを書き直しました(コピー/ペーストしてください)、私は価値を書くつもりはありませんでした!= null、ハ!あなたの答えは他の男のものと本質的に同じです。助けてくれてありがとう! – erodewald

+1

これはよくあることですが、私は彼/彼女をコピーしたようではありません。私は実際には答えが投稿される前にこれを考えましたが、コンピュータの再起動のためにこのコードを時間内に取得できませんでした。どういたしまして。 –

+1

本当にあなたは同時に投稿するようです。受け入れられなかった理由を説明してください。 – erodewald

関連する問題