依赖属性回调不起作用

发布于 2024-11-18 19:25:51 字数 595 浏览 8 评论 0原文

我有以下代码:

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 have the following code:

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

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

Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

百合的盛世恋 2024-11-25 19:25:51

您的代码是正确的,但是 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.

不交电费瞎发啥光 2024-11-25 19:25:51

将 DP 设为公共静态只读。在XAML中设置值时,不使用包装器,直接使用DP。所以,它必须是公开的。

但是......显然你是从代码中设置它的?在这种情况下,我不知道出了什么问题......但你总是可以尝试。

Make the DP public static readonly. When setting the value in XAML, the wrapper is not used, the DP is used directly. So, it has to be public.

But...apparently you are setting it from within code? In that case, i don't know what's wrong...but you can always try.

绿光 2024-11-25 19:25:51

我不知道这个问题是否已解决,但如果您在使用它的 XAML 文件中设置值,则在某些情况下,程序代码默认值将优先,并且永远不会因在最初是 XAML。所以删除默认值-1所以

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

变成

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

I don't know if this was ever solved or not but if you are setting the value in the XAML file that uses it, there are certain circumstances where the proceedural code default value will take precedent and it will never fire from being set in the XAML initially. So remove the default value of -1 so

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

becomes

private static readonly DependencyProperty IDProperty = DependencyProperty.Register(
       "ID", typeof(int), typeof(DetailDataControl), new PropertyMetadata( new PropertyChangedCallback(IDChanged)));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文