2016-09-28 140 views
1

我需要将WPF DatePicker中DatePickerTextBox的字符串格式更改为“mm/dd/yyyy”中的“dd/MM/YYYY”。 我想让用户手动添加日期。 当我在TextBox中以这种格式输入时:“mm/dd/yyyy”是DatePicker更改中的日期。但是当我以这种格式输入时:“dd/MM/yyy”我收到错误。 我想:的StringFormat = 'DD/MM/YYYY',但它并没有帮助:自定义WPF DatePicker掩码

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

enter image description here