【问题标题】:Turn invalid date into standard default date "01/01/1900" wpf datepicker c#将无效日期转换为标准默认日期“01/01/1900” wpf datepicker c#
【发布时间】:2018-07-30 07:58:25
【问题描述】:

如果我在 WPF 日期选择器“2018 年 2 月 30 日”或“2018 年 14 月 9 日”或任何无效日期中写入,它应该转换为标准默认日期“01/01/1900”,如果可能的话,还给messgaebox“无效日期” ”。 我的 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>

我的 Datepicker 的附加输出

如果我写的日期无效,那么它应该填充消息框并设置为默认日期“01/01/1900”。

【问题讨论】:

    标签: c# wpf datepicker


    【解决方案1】:

    如果你按照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

    如果您想要 01/01/1990,您可以将后者换成您自己的绑定或 DateTime.MinValue(如果您想要表明它不是正确的日期)?

    至于对用户的反馈,您可能会采用类似的方式

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

    ^ 表示如果绑定因任何原因(例如无效日期)失败,它应该回退到您将绑定设置为的任何内容。在本例中为 DateTime.Today。

    也许您可以通过在日期无效时可见的 DateTime 旁边放置一个标签来将信息返回给用户?还是玩转 DateTime 组件的 HintText?

    希望这是您所追求的解决方案!

    【讨论】:

    • sry,这对我不起作用。如果我在 datepicker 中写入无效日期并按 tab,然后注意这个绑定日发生。你能帮我解决一下吗
    【解决方案2】:

    您有一个LostFocus 事件处理程序:DatePicker1_LostFocus

    您可以在事件处理程序中添加您的验证逻辑:

    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();
            }
        }
    }
    
    • 以上代码获取用户输入的文本值
    • 验证是否为有效值
    • 如果验证失败,则会显示消息并重置值

    对于无效日期:

    重置后:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多