【问题标题】:Set culture for a WPF DateTimePicker validation为 WPF DateTimePicker 验证设置区域性
【发布时间】:2013-05-10 01:27:13
【问题描述】:

我在 WPF 应用程序上使用 DateTimePicker。 我想将 .Text 属性绑定到 SelectedDate,所以我使用这样的绑定:

<DatePicker x:Name="DateTimePicker_Date"
              Text="{Binding dDate, Mode=TwoWay,
                     UpdateSourceTrigger=PropertyChanged,
                     TargetNullValue='',
                     ValidatesOnDataErrors=True}"
              TabIndex="3"
              Grid.Column="1" />

我的问题是我使用欧洲文化,所以:DAY/MONTH/YEAR 而不是 MONTH/DAY/YEAR,所以如果我输入:14/02/2013,就会出现验证错误!

我该如何解决这个问题?

【问题讨论】:

    标签: c# wpf xaml binding cultureinfo


    【解决方案1】:

    已解决:

    只需添加一个:

    this.Language = XmlLanguage.GetLanguage("fr-FR");
    

    在窗口的代码隐藏中:)

    【讨论】:

      【解决方案2】:

      非常热门的话题。我一直在寻找答案,我想我的决定会适合你。 创建窗口时,所有控件都采用系统的默认区域性。 您更改属性 SelectedDate = "{Binding MyDate, StringFormat =" ..... "}" SelectedDateFormat = "Short" 也许您会看到某种您需要的格式,但它不适用于手动编辑输入。 为此,您需要在类初始化开始时设置文化

      public MyWPFWindow()
          {
              InitializeComponent();        
              /// <summary>
              /// Change ShortDate on Culture for this <see cref="MyWPFWindow"/>
              /// </summary>
              CultureInfo cd = CultureInfo.CreateSpecificCulture(CultureInfo.CurrentCulture.Name);
              cd.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
              Thread.CurrentThread.CurrentCulture = cd;
          }
      

      【讨论】:

        猜你喜欢
        • 2011-07-11
        • 1970-01-01
        • 1970-01-01
        • 2016-03-12
        • 1970-01-01
        • 1970-01-01
        • 2012-03-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多