2009-08-28 49 views
5

文字列依存プロパティ(SearchText)を更新すると、コレクション依存プロパティ(結果)を更新する必要があります。
マイコレクションDP:依存関係プロパティを別のものから更新する

public IEnumerable<string> Results{ 
    get { return (IEnumerable<string>) GetValue(ResultsProperty); } 
    set { SetValue(ResultsProperty, value); } 
} 
public static readonly DependencyProperty ResultsProperty= 
DependencyProperty.Register("Results", typeof(IEnumerable<string>), typeof(MainWindowVM), new UIPropertyMetadata(new List<string>())); 

私は運でこれを試してみました。私はResults = ....ラインにブレークポイントを置き、決してヒットしませんでした。

public string SearchText{ 
    get { return (string) GetValue(SearchTextProperty); } 
    set { 
    Results = 
      from T in Tree.GetPeople(value) 
      select T.FullName; 
    SetValue(SearchTextProperty, value); 
    } 
} 
public static readonly DependencyProperty SearchTextProperty= 
DependencyProperty.Register("SearchText", typeof(string), typeof(MainWindowVM), new UIPropertyMetadata("")); 

XAML:

<TextBox DockPanel.Dock="Top" Text="{Binding SearchValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

<ListBox DockPanel.Dock="Top" ItemsSource="{Binding NameResults}" SelectedItem="{Binding Search}" />

答えて

7

XAMLまたは結合することによって依存関係プロパティを設定する場合、ランタイムは常にインスタンスプロパティの別名をバイパスし、直接GetValueSetValueを呼び出します。このため、インスタンス・セッターが呼び出されていないためです。

プロパティの変更時に呼び出される依存関係プロパティを持つメソッドを登録することをお勧めします。これは、依存プロパティのPropertyMetadataを作成するときに最も簡単に実行されます。

次の例は、あなたが探していることをしていると思います。この例では、私のクラスには、FirstとSecondとしてエイリアスされた2つのdepencencyプロパティがあります。 Firstの値を設定すると、変更ハンドラが呼び出され、Secondの値が設定されます。

public class DependencyPropertyTest : DependencyObject 
{ 
    public static readonly DependencyProperty FirstProperty; 
    public static readonly DependencyProperty SecondProperty; 

    static DependencyPropertyTest() 
    { 
    FirstProperty = DependencyProperty.Register("FirstProperty", 
                typeof(bool), 
                typeof(DependencyPropertyTest), 
                new PropertyMetadata(false, FirstPropertyChanged)); 

    SecondProperty = DependencyProperty.Register("SecondProperty", 
               typeof(string), 
               typeof(DependencyPropertyTest), 
               new PropertyMetadata(null)); 
    } // End constructor 

    private bool First 
    { 
    get { return (bool)this.GetValue(FirstProperty); } 
    set { this.SetValue(FirstProperty, value);  } 

    } // End property First 

    private string Second 
    { 
    get { return (string)this.GetValue(SecondProperty); } 
    set { this.SetValue(SecondProperty, value);   } 

    } // End property Second 

    private static void FirstPropertyChanged(DependencyObject dependencyObject, 
              DependencyPropertyChangedEventArgs ea) 
    { 
    DependencyPropertyTest instance = dependencyObject as DependencyPropertyTest; 

    if (instance == null) 
    { 
     return; 
    } 

    instance.Second = String.Format("First is {0}.", ((bool)ea.NewValue).ToString()); 

    } // End method FirstPropertyChanged 
} // End class DependencyPropertyTest 

私はそれが役に立てば幸い。

+0

-1、値の代入ではなくSetCurrentValueのに使用されますので、これは 'Second'プロパティにバインド解除されます(参照http://stackoverflow.com/質問/ 4230698/whats-the-difference-between-dependency-property-setvalue-setcurrentvalue)。 – Benlitz

+3

真実今日、私はあなたを与えます。しかし、この回答が書かれた時点で、.NET Framework 3.5はSetCurrentValueメンバーを持たない最新のバージョンでした。 (ref:https://msdn.microsoft.com/en-us/library/system.windows.dependencyobject_members(v=vs.90).aspx) 近代化のためのコメントや追加回答が必要ですが、IMHO下降票を保証するものではありません。 –

5

私がコメントしたように、単純に割り当てを行うのではなく、SetCurrentValueメソッドを使用しなければならないという点を除いて、現在受け入れられている回答は原則的に正しいです。詳細については、this answerを参照してください。ここで

は、この修正プログラムと同じコードです:

public class DependencyPropertyTest : DependencyObject 
{ 
    public static readonly DependencyProperty FirstProperty; 
    public static readonly DependencyProperty SecondProperty; 

    static DependencyPropertyTest() 
    { 
    FirstProperty = DependencyProperty.Register("FirstProperty", 
                typeof(bool), 
                typeof(DependencyPropertyTest), 
                new PropertyMetadata(false, FirstPropertyChanged)); 

    SecondProperty = DependencyProperty.Register("SecondProperty", 
               typeof(string), 
               typeof(DependencyPropertyTest), 
               new PropertyMetadata(null)); 
    } // End constructor 

    private bool First 
    { 
    get { return (bool)this.GetValue(FirstProperty); } 
    set { this.SetValue(FirstProperty, value);  } 

    } // End property First 

    private string Second 
    { 
    get { return (string)this.GetValue(SecondProperty); } 
    set { this.SetValue(SecondProperty, value);   } 

    } // End property Second 

    private static void FirstPropertyChanged(DependencyObject dependencyObject, 
              DependencyPropertyChangedEventArgs ea) 
    { 
    DependencyPropertyTest instance = dependencyObject as DependencyPropertyTest; 

    if (instance == null) 
    { 
     return; 
    } 

    // SetCurrentValue should be used here! 
    instance.SetCurrentValue(SecondProperty, 
     String.Format("First is {0}.", ((bool)ea.NewValue).ToString()); 

    } // End method FirstPropertyChanged 
} // End class DependencyPropertyTest 
+0

コメントする必要があります! –

関連する問題