WPF xml:lang/Language binding

女生的网名这么多〃 提交于 2019-12-21 20:56:58

问题


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?


回答1:


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}}">



回答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}" />


来源:https://stackoverflow.com/questions/5606789/wpf-xmllang-language-binding

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!