2009-08-28 100 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 。正因爲如此,你的實例setter沒有被調用。

你可能希望考慮做的是註冊一個具有依賴項屬性的方法,當屬性改變時它將被調用。爲依賴項屬性創建PropertyMetadata時,最容易完成此操作。

我相信下面的例子會做你正在做的事情。在這個例子中,我的課有兩個依賴屬性,別名爲First和Second。當我爲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,這將打破對'秒'屬性綁定的SetValue,因爲將被用來代替SetCurrentValue(見http://stackoverflow.com/問題/ 4230698 /什麼最差之間的依賴關係,財產的setValue-setcurrentvalue)。 – Benlitz 2014-05-12 09:42:11

+3

今天是真的,我會給你的。但是,在編寫此答案時,.NET Framework 3.5是最新版本,沒有SetCurrentValue成員。 (ref:https://msdn.microsoft.com/en-us/library/system.windows.dependencyobject_members(v=vs.90).aspx) 很值得現代化的評論或額外的答案,但恕我直言它不保證否決票。 – 2015-02-14 21:18:32

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

應該是一個評論! – 2015-02-06 13:49:43