Use “real” CultureInfo.CurrentCulture in WPF Binding, not CultureInfo from IetfLanguageTag

前端 未结 9 870
余生分开走
余生分开走 2020-11-29 05:19

In my case:

I have a TextBlock Binding to a property of type DateTime. I want it to be displayed as the Regional settings of the User says.



        
9条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-29 05:49

    This is an extension of answer from aKzenT. They proposed that we should create a subclass of Binding class and set the ConverterCulture to CurrentCulture. Even though the answer is very straight forward, I feel some people may not be very comfortable implementing it, so I am sharing the code version of aKzenT's answer with an example of how to use it in XAML.

    using System;
    using System.Globalization;
    using System.Windows.Data;
    
    namespace MyWpfLibrary
    {
        public class CultureAwareBinding : Binding
        {
            public CultureAwareBinding()
            {
                ConverterCulture = CultureInfo.CurrentCulture;
            }
        }
    }
    

    Example of how to use this in XAML

    1) You need to import your namespace into your XAML file:

    
    

    2) Real world usage of the CultureAwareBinding

    
    

提交回复
热议问题