How to set Silverlight CurrentUICulture/CurrentCulture correctly?

前端 未结 2 1926
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-19 07:07

I\'m working on a SL5 app with C# and I\'m looking to internationalize it. I found the following to set the UI culture:

var culture = new CultureInfo(Thread.         


        
2条回答
  •  一向
    一向 (楼主)
    2020-12-19 07:17

    Thanks to eandersson I came up with this extension for specific controls. I had a problem with my decimal input, parsing and validation. Somewhere in the way there was this InvariantCulture with '.' as separator instead of ','. It can be easily modifed to set up specific culture.

    public class ElementCultureExtension
    {
        public static bool GetForceCurrentCulture( DependencyObject obj )
        {
            return (bool)obj.GetValue( ForceCurrentCultureProperty );
        }
    
        public static void SetForceCurrentCulture( DependencyObject obj, bool value )
        {
            obj.SetValue( ForceCurrentCultureProperty, value );
        }
    
        public static readonly DependencyProperty ForceCurrentCultureProperty =
            DependencyProperty.RegisterAttached(
                "ForceCurrentCulture", typeof( bool ), typeof( ElementCultureExtension ), new PropertyMetadata( false, OnForceCurrentCulturePropertyChanged ) );
    
        private static void OnForceCurrentCulturePropertyChanged(
            DependencyObject d,
            DependencyPropertyChangedEventArgs e )
        {
            var control = (FrameworkElement)d;
            if( (bool)e.NewValue )
            {
                control.Language = XmlLanguage.GetLanguage( Thread.CurrentThread.CurrentCulture.Name );
            }
        }
    }
    

    In Xaml:

    
    

提交回复
热议问题