【发布时间】:2013-01-06 07:42:11
【问题描述】:
我正在玩 WPF 用户控件并有以下问题:为什么在将属性设置为 DependencyProperty 后,属性初始化/赋值的行为会发生变化?
让我简单说明一下:
考虑将此代码用于UserControl 类:
public partial class myUserControl : UserControl
{
private string _blabla;
public myUserControl()
{
InitializeComponent();
_blabla = "init";
}
//public static DependencyProperty BlaBlaProperty = DependencyProperty.Register(
// "BlaBla", typeof(string), typeof(UserControlToolTip));
public string BlaBla
{
get { return _blabla; }
set { _blabla = value; }
}
}
这就是 UserControl 在 XAML 文件中的初始化方式:
<loc:myUserControl BlaBla="ddd" x:Name="myUsrCtrlName" />
我遇到的问题是 set { _blabla = value; } 仅在 DependencyProperty 声明被注释掉时才被调用(根据这个例子)。但是,当 DependencyProperty 行成为程序的一部分时,set { _blabla = value; } 行不再被系统调用。
有人可以向我解释一下这种奇怪的行为吗?
谢谢一百万!
【问题讨论】:
标签: wpf user-controls dependency-properties