自定义WPF DatePicker掩码 [英] Customizing WPF DatePicker Mask
问题描述
我需要将WPF DatePicker中的DatePickerTextBox的字符串格式更改为由 mm / dd / yyyy插入的 dd / MM / YYYY。
我想允许用户手动添加日期。
当我以以下格式在TextBox中输入时: mm / dd / yyyy DatePicker更改中的日期。但是,当我以以下格式输入时: dd / MM / yyy,我得到了一个错误。我尝试过的
:StringFormat ='dd / MM / yyyy',但没有帮助:
I need to change the string format of the DatePickerTextBox in the WPF DatePicker to "dd/MM/YYYY" insted of "mm/dd/yyyy". I want to allow users to add a date manually. When i'm typing in TextBox in this format: "mm/dd/yyyy" the date in DatePicker Changing. But when i'm typing in this format: "dd/MM/yyy" i'm getting an error. I tried: StringFormat='dd/MM/yyyy' but it didn't help:
<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>
推荐答案
您在<$ c $上遇到异常c> 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)'
因此,提供一个自定义转换器可以解决问题:
So, providing a custom converter solves the problem:
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掩码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!