0

我有一个 WPF 应用程序,其中 WPF Toolkit DatePicker 控件绑定到 DateTime 变量,如下所示:

<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" />

我的代码:

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 
      }
}

但是当我运行它时,我收到错误消息“字符串未被识别为有效的日期时间。”

如果有任何错误,我该如何解决?谢谢。

4

1 回答 1

0

更改SelectedDate.ToString()DateTime.TryParse(clndHireDate.Text)查看它是否是有效的日期时间。

于 2012-11-10T23:35:25.320 回答