wpf - 依赖属性回调不起作用

标签 wpf dependency-properties

我有以下代码:

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 is called),但它没有调用 IDChanged。

为什么?

最佳答案

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

关于wpf - 依赖属性回调不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6584463/

相关文章:

c# - ItemsControl 和 Canvas - 只有第一个项目可见

c# - DependencyProperties 未按预期在 UserControl 中触发回调

c# - 在 ItemsControl : how to Bind element of ItemSource to x:Name of UserControl? 内具有依赖属性的 UserControl

wpf - 依赖属性优先级。我希望本地值可以被覆盖

c# - ListView(WPF)中的分隔符?

C#:将音频文件从服务器流式传输到客户端

C# 在 TextBox 中从右到左

c# - 何时在 WPF 中使用 Rx 处理鼠标事件

c# - WPF 设置用户控件依赖属性

c# - 将属性注册为 DependencyProperty