【发布时间】:2013-07-13 11:54:47
【问题描述】:
我有一个用户控件。我想在我的用户控件中创建一个可绑定属性。我创建一个 DependencyProperty 如下:
public static readonly DependencyProperty DateProperty =
DependencyProperty.Register("Date", typeof(DateTime), typeof(DaiesContainer),
new UIPropertyMetadata(DateTime.Now));
public DateTime Date
{
get { return (DateTime)GetValue(DateProperty); }
set
{
SetValue(DateProperty, value);
}
}
然后我在我的 XAML 中使用它:
<ctrls:DaiesContainer Date="{Binding Date, Mode=OneWay}"/>
在我的 ViewModel 中,调用了 Date 属性的 get 方法。但在我的用户控件中,日期属性未设置为值。
【问题讨论】:
-
您是否在 XAML 中将
DateTime插入到 compatible pattern 中? -
怎么知道属性没有设置?您尚未使用依赖项属性的元数据注册 PropertyChangedCallback。请注意,在 XAML 中设置属性时不会调用 CLR 包装器的设置器,如 here 所述。
-
@Clemens 谢谢,这很好,但我必须调用非静态方法。