Silverlight 4 MVVM:在自定义日期时间控件中绑定 SelectedDate 会导致错误
我有一个自定义日期时间控件。 当我绑定其 SelectedDate 属性时,会出现以下错误:
System.Windows.Markup.XamlParseException:设置属性“GenericControls.CustomDatePickerControl.SelectedDate”引发异常。 [行:44 位置:131] ---> System.ArgumentException:“System.Windows.Data.Binding”类型的对象无法转换为“System.Nullable`1[System.DateTime]”类型。
背后的代码:
public partial class CustomDatePickerControl : UserControl
{
#region Properties
public DateTime? SelectedDate
{
get
{
return (DateTime?)dtpFromDate.GetValue
(DatePicker.SelectedDateProperty);
}
set
{
dtpFromDate.SetValue(DatePicker.SelectedDateProperty, value);
}
}
#endregion
#region Initialization
public CustomDatePickerControl()
{
InitializeComponent();
Loaded += OnLoaded;
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
Binding binding = new Binding("Text");
binding.Source = txtDate;
binding.Mode = BindingMode.TwoWay;
binding.Converter = (IValueConverter)Application.Current
.Resources["StringToDateConverter"];
dtpFromDate.SetBinding(DatePicker.SelectedDateProperty, binding);
}
#endregion
#region EventHandlers
private void txtDate_GotFocus(object sender, RoutedEventArgs e)
{
if (txtDate.Text == "To Date")
{
txtDate.Text = "";
}
}
private void txtDate_LostFocus(object sender, RoutedEventArgs e)
{
if (txtDate.Text == "")
{
txtDate.Text = "From Date";
}
}
#endregion
}
XAML:
<Grid x:Name="LayoutRoot" Background="Transparent">
<StackPanel Orientation="Horizontal" Background="Transparent">
<TextBox x:Name="txtDate" Text="From Date:" Width="74" Style="{StaticResource CommonTextBox}" Margin="3,0,0,0" LostFocus="txtDate_LostFocus" GotFocus="txtDate_GotFocus" MaxLength="10" />
<sdk:DatePicker Name="dtpFromDate" Grid.Column="6" Height="23" Width="25" Style="{StaticResource DatePickerStyle}" />
</StackPanel>
</Grid>
视图:
ViewModel:
public DateTime? DiarySelectedDate
{
get
{
return _selectedDate;
}
set
{
if (_selectedDate == value)
{
return;
}
_selectedDate = value;
RaisePropertyChanged("DiarySelectedDate");
}
}
非常感谢任何帮助, 提前致谢, 克鲁维
i have a custom datetime control.
When i bind its SelectedDate property it result with the following error:
System.Windows.Markup.XamlParseException: Set property 'GenericControls.CustomDatePickerControl.SelectedDate' threw an exception. [Line: 44 Position: 131] ---> System.ArgumentException: Object of type 'System.Windows.Data.Binding' cannot be converted to type 'System.Nullable`1[System.DateTime]'.
The code behind:
public partial class CustomDatePickerControl : UserControl
{
#region Properties
public DateTime? SelectedDate
{
get
{
return (DateTime?)dtpFromDate.GetValue
(DatePicker.SelectedDateProperty);
}
set
{
dtpFromDate.SetValue(DatePicker.SelectedDateProperty, value);
}
}
#endregion
#region Initialization
public CustomDatePickerControl()
{
InitializeComponent();
Loaded += OnLoaded;
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
Binding binding = new Binding("Text");
binding.Source = txtDate;
binding.Mode = BindingMode.TwoWay;
binding.Converter = (IValueConverter)Application.Current
.Resources["StringToDateConverter"];
dtpFromDate.SetBinding(DatePicker.SelectedDateProperty, binding);
}
#endregion
#region EventHandlers
private void txtDate_GotFocus(object sender, RoutedEventArgs e)
{
if (txtDate.Text == "To Date")
{
txtDate.Text = "";
}
}
private void txtDate_LostFocus(object sender, RoutedEventArgs e)
{
if (txtDate.Text == "")
{
txtDate.Text = "From Date";
}
}
#endregion
}
The XAML:
<Grid x:Name="LayoutRoot" Background="Transparent">
<StackPanel Orientation="Horizontal" Background="Transparent">
<TextBox x:Name="txtDate" Text="From Date:" Width="74" Style="{StaticResource CommonTextBox}" Margin="3,0,0,0" LostFocus="txtDate_LostFocus" GotFocus="txtDate_GotFocus" MaxLength="10" />
<sdk:DatePicker Name="dtpFromDate" Grid.Column="6" Height="23" Width="25" Style="{StaticResource DatePickerStyle}" />
</StackPanel>
</Grid>
The view:
The ViewModel:
public DateTime? DiarySelectedDate
{
get
{
return _selectedDate;
}
set
{
if (_selectedDate == value)
{
return;
}
_selectedDate = value;
RaisePropertyChanged("DiarySelectedDate");
}
}
Any help is much appreciated,
Thanks in advance,
Kruvi
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我注意到这是一个已回答的问题。
但我想通知一下我最近开始创建的 Silverlight 5 控制工具包的此链接。它包含(以及其他控件)一个 DateTimeBox 控件,您可以在同一控件中处理日期和时间。目前,它仍在开发中,但应该可以用于大多数场景。
I notice this is an answered question.
But I would like to notify about this link to a Silverlight 5 control toolkit I have recently started creating. It contains (among other controls) a DateTimeBox control in which you can handle both date and time within the same control. At this point, it is still under development, but it should be usable for most scenarios.
SelectedDate 必须是依赖属性。 MSDN
更新
如果您希望该更改有效,请将 SelectedDate 声明为依赖项属性,并将 OnSelectedChanged(在 SelectedDate 依赖项属性更改时调用)将 NewValue 设置为布局中的 dtpFromDate 控件。我不确定这是否是好的设计方法,但它可以解决您的问题。
SelectedDate must be dependency property. MSDN
Update
If you want that will work change declare SelectedDate as dependency property and OnSelectedChanged (invoked on SelectedDate dependency property changed) set NewValue to dtpFromDate control in layout. I'm not sure that it will be good design approach but it solve your problem.
有什么理由不能只在 XAML 中进行日期绑定吗?
Any reason you can't just do the date binding in XAML?