依赖项属性回调不起作用 [英] Dependency property callback does not work

查看:84
本文介绍了依赖项属性回调不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

private static readonly DependencyProperty IDProperty = DependencyProperty.Register(
           "ID", typeof(int), typeof(DetailDataControl), new PropertyMetadata(-1, new PropertyChangedCallback(IDChanged)));

    public int ID
    {
        get { return (int)GetValue(IDProperty); }
        set { SetValue(IDProperty, value); }
    }

    private static void IDChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
         // Do something here!  
    }

我可以看到,当我更改ID时,SetValue(IPproperty行被称为),但不会调用IDChanged。

I can see that when I change ID, the line SetValue(IPproperty is called), but it doesn't call the IDChanged.

为什么?

推荐答案

您的代码是正确的,但是PropertyChanged回调在更改之前不会被调用。尝试在连续的代码行中将该属性更改为两个不同的值,并设置一个断点,您可以看到它被命中了。我相信它会设置为-1,因此不会被调用。

Your code is correct, however PropertyChanged callback will not be called until it has changed. Try changing the property to two different values in consecutive lines of code and have a break point you can see that it's been hit. I believe it's set to -1 and hence it isn't called.

这篇关于依赖项属性回调不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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