我正在为WPF编写自定义的DateTimePicker类,但我似乎无法从父级获取绑定值。
父:
<cc:DateTimePicker DTSelectedDate="{Binding RA_EFFECTIVE_DATE}" />
子:
public partial class DateTimePicker : UserControl
{
public DateTime DTSelectedDate { get; set; }
public static readonly DependencyProperty DateProperty = DependencyProperty.Register
(
"DTSelectedDate",
typeof(DateTime),
typeof(DateTimePicker)
);
public DateTimePicker()
{
InitializeComponent();
this.DataContext = this;
}
}
儿童XAML:
<DatePicker SelectedDate="{Binding DTSelectedDate}" Width="150"/>
我在构造函数中使用调试器停止了,但似乎无法从父级获取任何值。
答案 0 :(得分:1)
public static readonly DependencyProperty SelectedDateProperty =
DependencyProperty.Register("SelectedDate", typeof(DateTime), typeof(DateTimePicker));
public DateTime SelectedDate
{
get { return (DateTime)GetValue(SelectedDateProperty); }
set { SetValue(SelectedDateProperty, value); }
}
同时编辑您的孩子XAML:
<UserControl
// the usual stuff that is already there
x:Name="Me">
<DatePicker SelectedDate="{Binding ElementName=Me, Path=SelectedDate}"
...