自定义 WPF DatePicker 掩码

Posted

技术标签:

【中文标题】自定义 WPF DatePicker 掩码【英文标题】:Customizing WPF DatePicker Mask 【发布时间】:2017-02-06 17:49:21 【问题描述】:

我需要将 WPF DatePicker 中 DatePickerTextBox 的字符串格式更改为“mm/dd/yyyy”的“dd/MM/YYYY”。 我想允许用户手动添加日期。 当我以这种格式输入 TextBox 时:“mm/dd/yyyy” DatePicker 更改中的日期。但是当我输入这种格式时:“dd/MM/yyy”我得到一个错误。 我试过:StringFormat='dd/MM/yyyy' 但没有帮助:

       <DatePicker x:Name="DateGviya" Text = "Binding NameOfMyProperty, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, TargetNullValue=''">
                <DatePicker.Resources>
                    <Style TargetType="x:Type DatePickerTextBox">
                        <Setter Property="Control.Template">
                            <Setter.Value>
                                <ControlTemplate>
                                    <TextBox x:Name="PART_TextBox" Text="Binding Path=SelectedDate, StringFormat='dd/MM/yyyy', RelativeSource=RelativeSource AncestorType=x:Type DatePicker" />
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </DatePicker.Resources>
            </DatePicker>

【问题讨论】:

【参考方案1】:

ConvertBack 方法出现异常:

Exception thrown: 'System.FormatException' in mscorlib.dll
Exception thrown: 'System.Reflection.TargetInvocationException' in mscorlib.dll
Exception thrown: 'System.FormatException' in mscorlib.dll
System.Windows.Data Error: 7 : ConvertBack cannot convert value '14/09/2016' (type 'String'). BindingExpression:Path=SelectedDate; DataItem='DatePicker' (Name='DateGviya'); target element is 'TextBox' (Name='PART_TextBox'); target property is 'Text' (type 'String') FormatException:'System.FormatException: String was not recognized as a valid DateTime.
   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at System.Convert.ToDateTime(String value, IFormatProvider provider)
   at System.String.System.IConvertible.ToDateTime(IFormatProvider provider)
   at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
   at MS.Internal.Data.SystemConvertConverter.ConvertBack(Object o, Type type, Object parameter, CultureInfo culture)
   at System.Windows.Data.BindingExpression.ConvertBackHelper(IValueConverter converter, Object value, Type sourceType, Object parameter, CultureInfo culture)'

所以,提供一个自定义转换器解决了这个问题:

public class MyCustomDateConverter : IValueConverter

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    
        if (value != null)
            return ((DateTime)value).ToString("dd/MM/yyyy hh:mm:ss tt", culture);

        return null;
    

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    
        return DateTime.ParseExact((string)value, "dd/MM/yyyy hh:mm:ss tt", culture);
    

XAML:

<Window x:Class="WpfApplication293.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApplication293"
    mc:Ignorable="d"
    Title="Window1" Height="350" Width="350">

<Window.Resources>

    <local:MyCustomDateConverter x:Key="Converter1" />

</Window.Resources>

<Window.DataContext>
    <local:MyViewModel/>
</Window.DataContext>

<Grid>
    <DatePicker x:Name="DateGviya" SelectedDate="Binding CurrentDate"  HorizontalAlignment="Center" VerticalAlignment="Top">
        <DatePicker.Resources>
            <Style TargetType="x:Type DatePickerTextBox">
                <Setter Property="Control.Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <TextBox x:Name="PART_TextBox" Text="Binding Path=SelectedDate, StringFormat='dd/MM/yyyy hh:mm:ss tt', RelativeSource=RelativeSource AncestorType=x:Type DatePicker, Converter=StaticResource Converter1" />
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </DatePicker.Resources>
    </DatePicker>
</Grid>

【讨论】:

以上是关于自定义 WPF DatePicker 掩码的主要内容,如果未能解决你的问题,请参考以下文章

WPF DatePicker:自定义显示文本

2021-08-14 WPF控件专题 DatePicker 控件详解

自定义 WPF DatePickerTextBox 模板

如何获得自定义日期?

Material2 - Datepicker自定义验证器值确实返回日期对象而不是字符串

在 ng-bootstrap datepicker 中将 CSS 类应用于自定义日期模板