【发布时间】:2012-03-28 23:08:14
【问题描述】:
如下所示,我正在我的一个用户控件中设置自定义属性。
C#
public static readonly DependencyProperty RuleType1 = DependencyProperty.Register
(
"RuleType1", typeof(int), typeof(EmailNotification), new PropertyMetadata(0)
);
public int RuleTypeId
{
get { return (int)GetValue(RuleType1); }
set { SetValue(RuleType1, value); }
}
XAML
<helper:VisitationHours RuleTypeId="2" />
这很好用,但在控件加载之前我似乎无法获得 RuleTypeId。它总是变为默认值 0。加载控件后,我可以访问我设置的任何值。如何在控件的构造函数中访问此值?谢谢
【问题讨论】:
-
XAML 相当于做:
new VisitationHours() { RuleTypeId = 2 };。请注意初始化程序,其中RuleTypeId没有通过 ctor 发送。尝试其他路径。
标签: wpf properties dependencies