Updating one property of a dependency on another

I have a string dependency property (SearchText), when updated, I need to update the collection dependency property (results).
My dp collection:

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>()));

I tried it with no luck. I set a breakpoint on the line Results = .... and it never came across.

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

+5
source share
2 answers

When setting dependency properties in XAML or binding, the runtime will always bypass the instance property alias and directly call GetValueand SetValue. Because of this, your instance installer is not called.

, , , , - , . 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

, .

+7

, , , SetCurrentValue . . .

:

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
+5

All Articles