为了使DependencyProperties绑定进行评估? [英] Order that DependencyProperties bindings are evaluated?

查看:139
本文介绍了为了使DependencyProperties绑定进行评估?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么决定的顺序对同一控制多个DepdencyProperties在

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

我使用的扩展WPF工具包 PropertyGrid中,并且都SelectedObject和PropertyDefinitions约束:

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

的问题是,从依赖属性的OnSelectedObjectChanged火灾,并在更改后的处理程序是参照PropertyDefinitions ,它被看到,因为空。如果我注释掉OnSelectedObjectChanged处理程序,然后我可以在调试时OnPropertyDefinitionsChanged被称为呼叫OnSelectedObjectChanged后看到的。

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



我面临的this论坛主题,但我问我如何改变这些属性将更新的顺序一个更一般的问题WPF

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.

我曾尝试在有不同的顺序多次调用NotifyPropertyChanged但这似乎并没有影响到这一点。我可能会导致以不同或者我应该只需要修改的PropertyGrid,这样它会为任何顺序工作?

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?

推荐答案

的简短的答案是,它是所有一个黑盒子,你不应该依赖于一个之前或之后另一个被评估。因此,最好的办法是修改的PropertyGrid因此它可以在属性设置的顺序无关。

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



此外,这将是不好的做法就靠这个。而这个怪癖可能只有当控件初始化的工作。更改 ActiveDataPoint 的DataContext 之后,可能会导致不同的顺序。

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.

这篇关于为了使DependencyProperties绑定进行评估?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆