验证WPF DatePicker控件时,我收到此错误字符串未被识别为有效日期时间 [英] I got this error string was not recognized as a valid datetime when I validate WPF DatePicker control
本文介绍了验证WPF DatePicker控件时,我收到此错误字符串未被识别为有效日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个WPF应用程序,其中有一个WPF Toolkit DatePicker控件绑定到DateTime变量,如下所示:
I have a WPF application, with a WPF Toolkit DatePicker control bound to a DateTime variable as following:
<myToolkit:DatePicker Grid.Row="4" Grid.Column="4" x:Name="clndrHiredate" HorizontalAlignment="Left" Height="20" Width="150" VerticalAlignment="Top" FontFamily="Tahoma" FontWeight="Light" FlowDirection="RightToLeft" SelectedDate="{Binding bindingDate,StringFormat='{}{0:MM/dd/yyyy}', Mode=TwoWay}" />
<Button Grid.Row="8" Grid.Column="4" Style="{StaticResource GlassButton}" x:Name="btnRead" Height="29" Content="Read" FontSize="14" Foreground="White" HorizontalContentAlignment="Center" FontFamily="Arial" FontWeight="Bold" Background="#FF47594E" Margin="64,13,31,196" Width="65" />
和我的代码:
And my code :
private DateTime bindingDate = DateTime.Now;
private void btnRead_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrEmpty(clndrHiredate.SelectedDate.ToString()))
{
errorText += MessageBox.show("Wrong");
state = true;
lblHireDate.Visibility = Visibility.Visible;
}
else
{
// complete
}
}
但是当我运行它时,出现错误消息无法将字符串识别为有效的日期时间."
如果有任何错误,我该如何解决?
谢谢.
[edit]固定的代码标签[/edit]
but when I run this , I got error message "string was not recognized as a valid datetime."
If there is any error, How do I fix this?
Thanks.
[edit]fixed code tags[/edit]
推荐答案
您无法绑定到私有字段;您需要公共属性(通知属性或依赖项属性)绑定到它.这是我的建议,希望对您有所帮助
在Window类中声明新的依赖项属性,如下所示:
You can not bind to private field; you need public property (notifying property or a dependency property) to bind to it. Here is my suggestion and I hope it may help you
Declare new dependency property in the Window class like this:
public DateTime BindingDate
{
get { return (DateTime)GetValue(DateBindingProperty); }
set { SetValue(DateBindingProperty, value); }
}
public static readonly DependencyProperty DateBindingProperty =
DependencyProperty.Register("DateBinding", typeof(DateTime), typeof(MainWindow), new FrameworkPropertyMetadata(DateTime.Now));
>
然后替换您的XAML代码,将SelectedDate绑定到此依赖项属性(BindingDate),如下所示:
Then replace your XAML code to bind the SelectedDate to this dependency property (BindingDate) like this:
<DatePicker Height="29" HorizontalAlignment="Left"
Margin="78,65,0,0" Name="datePicker1" VerticalAlignment="Top" Width="163">
<DatePicker.SelectedDate>
<Binding Path="BindingDate" Mode="TwoWay">
<Binding.RelativeSource>
<RelativeSource
Mode="FindAncestor"
AncestorType="{x:Type Window}"
AncestorLevel="1"/>
</Binding.RelativeSource>
</Binding>
</DatePicker.SelectedDate>
</DatePicker>
我这样解决了:
I solved this like that:
<mytoolkit:datepicker grid.row="4" grid.column="4" x:name="clndrHiredate" horizontalalignment="Left" height="20" xmlns:x="#unknown" xmlns:mytoolkit="#unknown">
Width="150" VerticalAlignment="Top" FontFamily="Tahoma" FontWeight="Light" FlowDirection="RightToLeft"/>
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.clndrHiredate.SelectedDate = Convert.ToDateTime(DateTime.Now.ToString("MM/dd/yyyy"));
}
private void btnRead_Click(object sender, RoutedEventArgs e)
{
if (clndrHiredate.SelectedDate == null)
errorText += MessageBox.show("Wrong");
state = true;
lblHireDate.Visibility = Visibility.Visible;
}
else
{
// complete
}
}</mytoolkit:datepicker>
[edit]添加了代码块[/edit]
这篇关于验证WPF DatePicker控件时,我收到此错误字符串未被识别为有效日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文