Order that DependencyProperties bindings are evaluated?

给你一囗甜甜゛ 提交于 2019-12-05 15:51:16

问题


What determines the order that multiple DepdencyProperties on the same control get evaluated in?

I am using the Extended WPF Toolkit PropertyGrid and have both SelectedObject and PropertyDefinitions bound:

<extToolkit:PropertyGrid AutoGenerateProperties="False" SelectedObject="{Binding ActiveDataPoint}" PropertyDefinitions="{Binding ActiveDataPoint.Properties}">

The problem is that the OnSelectedObjectChanged fires from the dependency property, and in that changed handler it is referencing PropertyDefinitions, which it is seeing as null. If I comment out the OnSelectedObjectChanged handler then I can see when debugging that OnPropertyDefinitionsChanged is called AFTER the call to OnSelectedObjectChanged.

public static readonly DependencyProperty PropertyDefinitionsProperty = DependencyProperty.Register( "PropertyDefinitions", typeof( PropertyDefinitionCollection ), typeof( PropertyGrid ), new UIPropertyMetadata( null, OnPropertyDefinitionsChanged ) );
public PropertyDefinitionCollection PropertyDefinitions
{
  get
  {
    return ( PropertyDefinitionCollection )GetValue( PropertyDefinitionsProperty );
  }
  set
  {
    SetValue( PropertyDefinitionsProperty, value );
  }
}

private static void OnPropertyDefinitionsChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
    Console.Write("I changed!");
}

public static readonly DependencyProperty SelectedObjectProperty = DependencyProperty.Register( "SelectedObject", typeof( object ), typeof( PropertyGrid ), new UIPropertyMetadata( null, OnSelectedObjectChanged ) );
public object SelectedObject
{
  get
  {
    return ( object )GetValue( SelectedObjectProperty );
  }
  set
  {
    SetValue( SelectedObjectProperty, value );
  }
}

private static void OnSelectedObjectChanged( DependencyObject o, DependencyPropertyChangedEventArgs e )
{
  PropertyGrid propertyInspector = o as PropertyGrid;
  if( propertyInspector != null )
    propertyInspector.OnSelectedObjectChanged( ( object )e.OldValue, ( object )e.NewValue );
}

The problem I am facing is discussed on this forum thread, but I am asking a more general WPF question of how I can change the order that these properties are updated.

I have tried having multiple calls to NotifyPropertyChanged in different orders but that doesn't seem to affect this. Can I cause the order to be different or should I just modify the PropertyGrid so that it will work for either order?


回答1:


The short answer is that it is all a black box and you should not rely on one being evaluated before or after another. So the best approach would be to modify the PropertyGrid so it works regardless of the order the properties are set.

The long answer is it looks like it depends on how the order that the bindings are specified. So you can do:

<extToolkit:PropertyGrid AutoGenerateProperties="False"
    PropertyDefinitions="{Binding ActiveDataPoint.Properties}"
    SelectedObject="{Binding ActiveDataPoint}"
    >

Instead of:

<extToolkit:PropertyGrid AutoGenerateProperties="False"
    SelectedObject="{Binding ActiveDataPoint}"
    PropertyDefinitions="{Binding ActiveDataPoint.Properties}"
    >

Again, it would be bad practice to rely on this. And this quirk may only work for when the control is initialized. Changes to ActiveDataPoint or the DataContext later, may result in a different order.




回答2:


And just one more contra-example to confirm what has been said already

...to never rely on the order of properties being applied

In a custom UserControl with defined DependencyProperty-ies (.NET 4.5 etc.) - as PropertyChangedCallbacks are called at initialization...

the actual order is determined from the order of "code behind definitions" (static fields)

...I'm guessing that has to do with the order of Registration.

In some other cases the order depends on how the properties are lined up in the XAML.



来源:https://stackoverflow.com/questions/9744366/order-that-dependencyproperties-bindings-are-evaluated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!