代码之家  ›  专栏  ›  技术社区  ›  Rob

从另一个更新依赖项属性

  •  5
  • Rob  · 技术社区  · 15 年前

    我有一个字符串依赖属性(searchText),更新后需要更新集合依赖属性(results)。
    我的收藏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>()));
    

    我没有运气就试过这个。我在结果处放置了一个断点。线和它从来没有被击中。

    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}" />

    2 回复  |  直到 10 年前
        1
  •  9
  •   Jesse Squire    15 年前

    在XAML中或通过绑定设置依赖属性时,运行时将始终绕过实例属性别名并直接调用 GetValue SetValue . 正是因为这样,才不会调用实例设置器。

    您可能希望考虑在依赖属性中注册一个方法,当该属性更改时将调用该方法。在创建 PropertyMetadata 对于依赖属性。

    我相信下面的例子可以实现你想要做的。在这个例子中,我的类有两个依赖属性,分别命名为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
    

    希望有帮助。

        2
  •  6
  •   Community Egal    7 年前

    正如我所评论的,目前接受的答案在原则上是正确的,除非它必须使用 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