我尝试创建自定义控件并遇到绑定问题。
这是我的财产:
public DateTime? SelectedDate
{
get
{
return (DateTime?)GetValue(SelectedDateProperty); ;
}
set
{
SetValue(SelectedDateProperty, value);
}
}
public static readonly DependencyProperty SelectedDateProperty = DependencyProperty.Register(
"SelectedDate",
typeof(DateTime?),
typeof(ValidationDateSelector),
new PropertyMetadata(null));
...
private void ChengeSelectedDate()
{
if (_day != 0 && _month != 0 && _year != 0)
{
SelectedDate = new DateTime(_year, _month, _day);
OnSelectedDateChenged();
}
}
...
public event EventHandler<EventArgs> SelectedDateChanged;
private void OnSelectedDateChenged()
{
if (SelectedDateChanged != null)
SelectedDateChanged(this, EventArgs.Empty);
}
在XAML中绑定:
<Ctrl:DateSelector x:Name="ValidationDateSelector"
SelectedDate="{Binding DateOfBirth, Mode=TwoWay}"
SelectedDateChanged="DatePicker_OnSelectedDateChanged"/>
所以Binding不起作用,但DateSelector.SelectedDate是prathent,我可以在代码中为DateOfBirth赋值。为什么?我的逻辑有什么不对?