从另一个更新一个依赖项属性

时间:2009-08-28 13:22:09

标签: c# wpf data-binding xaml

我有一个字符串依赖属性(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}" />

3 个答案:

答案 0 :(得分:9)

在XAML中设置依赖项属性或通过绑定时,运行时将始终绕过实例属性别名,并直接调用GetValueSetValue。正因为如此,您的实例设置器才被调用。

您可能希望考虑的是使用依赖项属性注册方法,该属性将在属性更改时调用。在为依赖项属性创建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

我希望有所帮助。

答案 1 :(得分:6)

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

答案 2 :(得分:0)

您在其依赖项属性上放置的顺序非常重要 放在类文件中的第一把拳头将被执行,然后再向下一个拳头

class A {
dependecy 1
dependecy 2 
dependecy 3
}

它将按照在类上设置的顺序进行调用,,因此只需对所需顺序进行依赖即可 如果1取决于2->首先放置2,则像这样放置1

class A {
dependecy 2 
dependecy 1
dependecy 3
}