1
votes

I'm trying to set as DatePicker locale the CurrentCulture, what I did so far:

XAML Definition

xmlns:glob="clr-namespace:System.Globalization;assembly=mscorlib"

DatePicker structure

<DatePicker Language="{Binding Source={x:Static glob:CultureInfo.CurrentCulture}}"  />

the problem's I got this exception:

System.Windows.Data Error: 1 : Cannot create default converter to perform 'one-way' conversions between types 'System.Globalization.CultureInfo' and 'System.Windows.Markup.XmlLanguage'. Consider using Converter property of Binding. BindingExpression:Path=; DataItem='CultureInfo' (HashCode=-1158415740); target element is 'DatePicker' (Name='MatchCalendarDate'); target property is 'Language' (type 'XmlLanguage') System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='it-IT' BindingExpression:Path=; DataItem='CultureInfo' (HashCode=-1158415740); target element is 'DatePicker' (Name='MatchCalendarDate'); target property is 'Language' (type 'XmlLanguage')

Note that I'm using as DatePicker control the MahApp.

2
you can do like this <DatePicker xml:lang="en-US"/>cuongtd
@Rise I've a multilanguage application, so each time that the app start, this need to initialize the DatePicker to the currentLocale in xamlil santino

2 Answers

2
votes

You are trying to set the Language property to a CultureInfo object and this won't work. You need to set it to a XmlLanguage.

Unfortunately you cannot set it to the language of the current culture in pure XAML, but you could easily do the exact same thing programmatically:

dp.Language = XmlLanguage.GetLanguage(System.Globalization.CultureInfo.CurrentCulture.IetfLanguageTag);

<DatePicker x:Name="dp" />

And this certainly does not break the MVVM pattern in any way since you set the exact same property in the exact same view.

0
votes

You can use attached property to change language like this

  public class AttachedProperties
    {
        public static readonly DependencyProperty SetLanguageProperty =
                DependencyProperty.RegisterAttached("SetLanguage", typeof(bool), typeof(AttachedProperties), new PropertyMetadata(false, OnSetLanguageChanged));

        private static void OnSetLanguageChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as DatePicker).Language = XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag);
        }
        public static bool GetSetLanguage(DependencyObject obj)
        {
            return (bool)obj.GetValue(SetLanguageProperty);
        }
        public static void SetSetLanguage(DependencyObject obj, bool value)
        {
            obj.SetValue(SetLanguageProperty, value);
        }
    }

in xaml

<DatePicker local:AttachedProperties.SetLanguage="True"/>