DependencyProperty未触发 [英] DependencyProperty not triggered

查看:76
本文介绍了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)。 / p>

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加载和依赖属性,以解释为何未调用设置程序。

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天全站免登陆