Праграма WPF наладжвае кароткую дату пры выкарыстанні stringformat={}{0:d} (WPF application customize shortdate when using stringformat={}{0:d})


問題描述

Праграма WPF наладжвае кароткую дату пры выкарыстанні stringformat={}{0:d} (WPF application customize shortdate when using stringformat={}{0:d})

We are trying to allow the app to have its culture set at runtime and optionally override the way dates are displayed.

We create our own culture with:

private CultureInfo CreateCulture()
    {
        // start with basic culture object
        CultureInfo culture = Sys.Workspace.ServerProperties.UICulture ?? new CultureInfo(Properties.Settings.Default.DefaultCulture);

        // override data format
        var dateFmt = Sys.Workspace.ServerProperties.DateFormatOverride;
        if (!string.IsNullOrEmpty(dateFmt))
        {
            culture.DateTimeFormat.ShortDatePattern = dateFmt;
            culture.DateTimeFormat.LongDatePattern = dateFmt;
            if (dateFmt.Contains("/")) culture.DateTimeFormat.DateSeparator = "/";
            if (dateFmt.Contains("‑")) culture.DateTimeFormat.DateSeparator = "‑";
        }

        return culture;
    }

Then in the initialization of the app we do:

        var culture = CreateCulture();

        // set ui culture
        Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = culture;

        // set WPF culture
        LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(culture.IetfLanguageTag)));

We set the "Sys.Workspace.ServerProperties.DateFormatOverride" value to be "dd‑MMM‑yyyy".  This doesn't seem to be respected by the bindings though.  For example:

  <TextBlock TextAlignment="Center" HorizontalAlignment="Stretch" 
      Text="{Binding NoteDate, StringFormat={}{0:d}}" />

The NoteDate is a DataColumn with type of DateTime but displays the default cultures shortdate pattern.

If I set the main culture to en‑GB I will get dd‑mm‑yyyy, if I set it to en‑US I will get mm‑dd‑yyyy.  

When I look at the Thread.CurrentThread.CurrentCulture and currentUICulture it is set correctly, i.e. it is en‑gb and the shortdatepattern and longdatepattern are dd‑MMM‑yyyy but the dates do not display in this format.

‑‑‑‑‑

參考解法

方法 1:

The reason is the FrameworkElement's don't look at the Thread.CurrentThread.CurrentCulture for globalisation info. 

Instead you have to define the culture manually for XAML elements...

Answered here

(by Jonmortware)

參考文件

  1. WPF application customize shortdate when using stringformat={}{0:d} (CC BY‑SA 3.0/4.0)

#.net-3.5 #datetime-format #wpf #uiculture






相關問題

如何在 C# 中搜索 pdf 中的文本(執行匹配) (How to search text (Exect match) in pdf in C#)

String.IsNullOrEmpty() (String.IsNullOrEmpty())

是否有可以綁定的可為空的日期選擇器? (Is there a nullable datepicker that I can bind to?)

具有自動命名屬性的通用組合框 (Generic ComboBox with automatically named properties)

在數據庫未定義的外鍵關係上配置實體框架 (Configuring Entity Framework on a Database Undefined Foreign Key Relationships)

我可以在 4.0 應用程序中引用 .NET 3.5 .DLL 嗎? (Can I reference a .NET 3.5 .DLL in a 4.0 app?)

如何在 ASP.NET、VB.NET 中解決這個會話問題? (How to tackle this session problem in ASP.NET,VB.NET?)

在 ADO.NET 數據服務中添加對查找表的引用 (Adding a reference to a lookup table in ADO.NET Data Services)

如何優化 Linq to Xml 查詢反對屬性? (How do I optimize a Linq to Xml query againist attributes?)

VS2005 和 LINQ (VS2005 and LINQ)

時區信息錯誤? (TimeZoneInfo error?)

與標準 C++ 相比,C++/CLI(以前稱為“託管 C++”)有哪些優勢? (What are the advantages of C++/CLI (formerly "Managed C++") over standard C++?)







留言討論