2009-08-28 99 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