【问题标题】:Getting notification when value source of a dependency property changes当依赖属性的值源发生变化时获取通知
【发布时间】:2014-01-20 09:09:36
【问题描述】:

有没有办法在依赖属性的值源发生变化时获得通知?

我创建了一个附加属性,用于设置将值继承给子元素的标签:

LabelProperty = DependencyProperty.RegisterAttached ("Label", typeof (string), typeof (Class1), 
            new FrameworkPropertyMetadata (null, FrameworkPropertyMetadataOptions.Inherits, OnLabelChanged));

我将 a 值设置为父元素的 Label 属性,从而有效地将其应用于所有子元素。在某些时候,子元素会设置与附加属性的本地值相同的值(在 XAML 中)。尽管值实际上并没有改变,但值源从 Inherits 变为 Local。

有没有办法捕捉价值源的这种变化?

【问题讨论】:

  • 属性创建中的Class1是指属性的拥有者,而不是可以分配给哪个对象。 SetXXX 和 GetXXX 静态方法接受 DependencyObject 作为参数。

标签: c# wpf dependency-properties


【解决方案1】:

优先级改变时没有回调之类的东西。

但是,您始终可以使用 DependencyPropertyHelper 类来请求当前优先级。

http://msdn.microsoft.com/en-us/library/system.windows.dependencypropertyhelper.getvaluesource%28v=vs.110%29.aspx

http://msdn.microsoft.com/en-us/library/system.windows.valuesource%28v=vs.110%29.aspx

顺便说一句,如果值是通过本地属性设置器设置的,您可以在此处附加您的自定义逻辑,表明您在本地调用了 SetValue。希望对您有所帮助。

编辑:

要跟踪您可以订阅强制的任何值设置。

    public static int GetMyProperty(DependencyObject obj)
    {
        return (int)obj.GetValue(MyPropertyProperty);
    }

    public static void SetMyProperty(DependencyObject obj, int value)
    {
        obj.SetValue(MyPropertyProperty, value);
    }

    // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty MyPropertyProperty =
        DependencyProperty.RegisterAttached("MyProperty", typeof(int), typeof(MainWindow), new UIPropertyMetadata(0, null, delegate(DependencyObject o, object value) { /*do something here*/ return value; }));

【讨论】:

  • 我知道 DependencyPropertyHelper.GetValueSource 但它对我没有多大帮助,因为我没有任何地方可以调用它。子属性的值是在 XAML 中设置的,所以我不能使用附加属性的 SetXXX 静态方法检查值源。
  • 是的,您可以使用 SetXXX。 XAML 中的设置值正是通过 SetXXX 方法。
  • 在 XAML 中设置附加属性的值不使用 SetXXX 方法(也提到了here)
  • 对不起,我一直以为我们在谈论会员。这是给你的代码。看看我的答案中的编辑。
【解决方案2】:

首先,感谢开发刺猬的帮助。

在对这个问题进行了几天的新研究之后,我认为这种情况没有解决方案。当值的源从 Inherits 更改为 Local 时,我无法找到一种方法来导致 WPF 引发属性更改事件(如果有人找到了这样做的方法,我会很高兴知道)。

使用 Coerce 机制的方向起初看起来很有希望,所以我尝试做这样的事情:

private static object CoeracLabel (DependencyObject d, object value)
{
    LabelProperty.DefaultMetadata.PropertyChangedCallback (d, new DependencyPropertyChangedEventArgs (LabelProperty, value, value));
    return value;
}

但是在摆弄这种方法半小时后,我也不得不放弃它,因为当调用强制方法时,值源还没有更新,所以我无法运行任何依赖它的逻辑。

鉴于此,我决定更改解决方案并删除属性值继承。现在创建的依赖属性没有 Inherits 标志,并且在 in 属性更改回调中,我尝试找到具有不同值的父元素。

string parentLabel = GetParentLabel (d);
.
.

private static string GetParentLabel (DependencyObject child)
    {
        string parentLabel = null;

        child = LayoutHelper.GetParent (child, false);

        LayoutHelper.FindLayoutOrVisualParentObject (child, x =>
        {
            string label = GetLabel (x);
            if (!String.IsNullOrEmpty (label))
            {
                parentLabel = label;
                return true;
            }

            return false;
        }, true);

        return parentLabel;
    }

【讨论】:

    猜你喜欢
    • 2010-09-19
    • 1970-01-01
    • 2012-04-13
    • 2011-01-15
    • 2016-11-18
    • 2011-04-03
    • 1970-01-01
    • 2019-12-17
    • 1970-01-01
    相关资源
    最近更新 更多