DependencyProperty 未触发 [英] DependencyProperty not triggered

查看:50
本文介绍了DependencyProperty 未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的用户控件中定义了这样的属性:

I defined property in my usercontrol like this:

 public bool IsSelected 
 {
     get { return (bool)GetValue(IsSelectedProperty); }
     set
     {
         SetValue(IsSelectedProperty, value);
         StackPanelDetails.Visibility = value ? Visibility.Visible : Visibility.Collapsed;
     }
 }

 public static readonly DependencyProperty IsSelectedProperty =
        DependencyProperty.Register("IsSelected", typeof (bool), typeof (ucMyControl));

但是当我在 xaml 中设置它的属性时,它想要触发它(set 不被调用).

But when I set its property in xaml, it want trigger it (set is not called).

 <DataTemplate><local:ucTopicItem IsSelected="False" /></DataTemplate>

可能是什么问题?

推荐答案

在 XAML 中设置属性时,不会调用依赖属性的设置器.WPF 将改为直接调用 SetValue 方法.

The setter of your dependency property will not be called when the property is set in XAML. WPF will instead call the SetValue method directly.

请参阅 MSDN XAML 加载和依赖属性,了解原因未调用 setter.

See MSDN XAML Loading and Dependency Properties for an explanation why the setter is not called.

您必须注册一个PropertyChangedCallback带有属性元数据.

You would have to register a PropertyChangedCallback with property metadata.

这篇关于DependencyProperty 未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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