简体   繁体   English

将无效日期转换为标准默认日期“ 01/01/1900” wpf datepicker C#

[英]Turn invalid date into standard default date “01/01/1900” wpf datepicker c#

If I write in WPF datepicker "Feb 30,2018" or "14/9/2018" or any invalid date it should convert into standard default date "01/01/1900" and if possible also give messgaebox "Invalid date". 如果我用WPF datepicker“ Feb 30,2018”或“ 14/9/2018”或任何无效日期编写,则应将其转换为标准默认日期“ 01/01/1900”,并在可能的情况下也给消息框输入“ Invalid date”。 My Code of XML is : 我的XML代码是:

 <DatePicker x:Name="DatePicker1" SelectedDateFormat="Short" Margin="10,10,10,0" Grid.ColumnSpan="5" 
                    IsTodayHighlighted="True"  RenderTransformOrigin="0.129,-2.84" PreviewTextInput="DatePicker1_PreviewTextInput" 
                    DateValidationError="DatePicker1_DateValidationError" 
                    DisplayDateStart="1/1/1990"  SelectedDate="{x:Static sys:DateTime.Now}"  LostFocus="DatePicker1_LostFocus" Height="28" VerticalAlignment="Top" CalendarOpened="DatePicker1_CalendarOpened" >
                    <DatePicker.Resources>
                        <Style TargetType="{x:Type DatePickerTextBox}">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate>
                                        <TextBox x:Name="PART_TextBox"
                                            Text="{Binding SelectedDate, RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}, StringFormat=dd-MMM-yyyy}" />
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </DatePicker.Resources>
                </DatePicker>

attached output of my Datepicker 我的Datepicker的附加输出 在此处输入图片说明

if i write invalid date then it should populate messagebox and set as default date "01/01/1900". 如果我写了无效的日期,那么它应该填充消息框并设置为默认日期“ 01/01/1900”。

How about if you go by the msdn recommendation and use 如果您按照msdn的建议去使用又如何呢?

SelectedDate="{Binding Day, TargetNullValue={x:Static system:DateTime.Now}}"

https://social.msdn.microsoft.com/Forums/vstudio/en-US/7db00560-633e-42cc-b49b-a755ac3e6e59/default-date-in-datepicker?forum=wpf https://social.msdn.microsoft.com/Forums/vstudio/en-US/7db00560-633e-42cc-b49b-a755ac3e6e59/default-date-in-datepicker?forum=wpf

And if you want 01/01/1990 you swap the latter to a binding of your own or DateTime.MinValue if you're after the effect to show that it's not a proper date? 如果想在1990年1月1日之后将其交换为您自己的绑定,或者将DateTime.MinValue替换为您自己的绑定(如果您想要显示不是正确的日期)?

As for feedback to the user you could perhaps go with something like 至于对用户的反馈,您也许可以选择类似

SelectedDate="{Binding Day, FallbackValue={x:Static sys:DateTime.Today}}"

^ means that if the binding fails for any reason (invalid date for instance), it should fall back to whatever you set the binding to. ^表示如果绑定由于任何原因而失败(例如无效的日期),则应退回到您设置绑定的目的。 In this case DateTime.Today. 在这种情况下,DateTime.Today。

Perhaps you can solve the information back to the user by putting a label next to the DateTime which is visible when the date is invalid? 也许您可以通过在日期时间旁边显示一个在日期无效时可见的标签来将信息返回给用户? Or play around with the HintText of the DateTime component? 还是使用DateTime组件的HintText?

Hopefully this is the type of solution you're after! 希望这是您追求的解决方案类型!

You have a LostFocus event handler: DatePicker1_LostFocus 您有一个LostFocus事件处理程序: DatePicker1_LostFocus

You can add your validation logic in the event handler: 您可以在事件处理程序中添加验证逻辑:

private void DatePicker1_LostFocus(object sender, RoutedEventArgs e)
{
    if (e.OriginalSource is TextBox textBox)
    {
        var val = textBox.Text;

        if(!DateTime.TryParse(val, out var dateTimeVal))
        {
            MessageBox.Show($"The entered value ({val}) is not valid.");
            textBox.Text = new DateTime(1900, 1, 1).ToShortDateString();
        }
    }
}
  • The above code gets the user entered text value 上面的代码获取用户输入的文本值
  • Validates whether it is valid value 验证是否为有效值
  • If validation fails, it shows the message and resets the value 如果验证失败,则显示消息并重置值

For invalid date: 对于无效的日期:

在此处输入图片说明

After reset: 重置后:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM