2

how can i bind Listbox's or texblock's Language attribute (or xml:lang attribute).

i want to show month names in the specific language setting

ex:

<TextBlock x:Name="Date" xml:lang="{Binding Lang}">
        <TextBlock.Text>
            <MultiBinding StringFormat=" {0:dd.MMM.yyyy}-{1:dd.MMM.yyyy}">
                <Binding Path="Date1"/>
                <Binding Path="Date2"/>
            </MultiBinding>
</TextBlock.Text>

result should be according to Lang property:

01.Apr.2011 - 01.Apr.2011 en-US

or 01.Nis.2011 - 02.Nis.2011 tr-TR

or ....

it gives XamlParseException : Language attribute cannot convert to System.Windows.Markup.XmlLanguage type (that is not exact Error Text. )

Any Idea?

idemirel
  • 309
  • 5
  • 19

2 Answers2

5

In the Startup event of the application, add this instruction:

FrameworkElement.LanguageProperty.OverrideMetadata(
    typeof(FrameworkElement),
    new FrameworkPropertyMetadata(
        XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));

It will override the default value of the Language property to the current culture, for the whole application.


EDIT: ok, I had misunderstood your question...

If you want to bind the Language property to a string containing the IetfLanguageTag, you need a converter:

[ValueConversion(typeof(string), typeof(XmlLanguage))]
public class IetfTagToXmlLanguageConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string tag = value as string;
        if (tag == null)
            return Binding.DoNothing;
        return XmlLanguage.GetLanguage(tag);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        XmlLanguage lang = value as XmlLanguage;
        if (lang == null)
            return Binding.DoNothing;
        return lang.IetfLanguageTag;
    }
}

Declare the converter in the XAML resources:

<local:IetfTagToXmlLanguageConverter x:Key="languageConverter" />

And use the converter in the binding:

<TextBlock Language="{Binding Lang, Converter={StaticResource languageConverter}}">
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • it is already overridden in StartUp. But i want to change all TextBlock Language property dynamically in the runtime. For Example; select a IetfLanguageTag value from a combobox. Thanks For the reply – idemirel Apr 09 '11 at 19:19
  • i am going to try it in a 5 min. Thank you – idemirel Apr 10 '11 at 08:34
  • 1
    @Thomas Levesque: Did this work for you? When I tried it, I got an exception saying `Binding for property 'Language' cannot use the target element's Language for conversion; if a culture is required, ConverterCulture must be explicitly specified on the Binding`. Not sure what this means since the value for `Language` can be set and changed without any problems – Fredrik Hedblad Aug 22 '11 at 20:44
  • This do not work, there comes a exception, like @FredrikHedblad wrote. – simmeone Aug 18 '21 at 09:18
2

you could create attached property and use it.

sealed class CultureBehavior
{
    public static DependencyProperty CultureProperty =
        DependencyProperty.RegisterAttached("Culture",
            typeof (string),
            typeof (CultureBehavior),
            new UIPropertyMetadata(OnCultureChanged));

    public static void SetCulture(FrameworkElement target, string value)
    {
        target.SetValue(CultureProperty, value);
    }

    public static string GetCulture(FrameworkElement target)
    {
        return (string) target.GetValue(CultureProperty);
    }

    private static void OnCultureChanged(DependencyObject target, DependencyPropertyChangedEventArgs args)
    {
        var element = target as FrameworkElement;
        if (element == null) return;

        element.Language = XmlLanguage.GetLanguage(args.NewValue.ToString());
    }
}

XAML

xmlns:local="clr-namespace:App.Utils"
....

<TextBlock Text="{Binding Repairs, StringFormat=c}"  local:CultureBehavior.Culture="{Binding CultureString}" />
Serhii Kyslyi
  • 1,745
  • 24
  • 43