WPF xml:lang/语言绑定

发布于 2024-10-31 02:43:42 字数 685 浏览 1 评论 0原文

我如何绑定 Listbox 或 texblock 的 Language 属性(或 xml:lang 属性)。

我想以特定语言设置显示月份名称

例如:

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

结果应根据 Lang 属性:

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

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

或者....

它给出 XamlParseException : Language attribute无法转换为System.Windows.Markup.XmlLanguage类型(这不是确切的错误文本。)

有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我早已燃尽 2024-11-07 02:43:42

在应用程序的 Startup 事件中,添加以下指令:

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

它将整个应用程序的 Language 属性的默认值覆盖为当前区域性。


编辑:好的,我误解了你的问题...

如果你想将 Language 属性绑定到包含 IetfLanguageTag 的字符串,你需要一个转换器:

[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;
    }
}

在中声明转换器XAML 资源:

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

并在绑定中使用转换器:

<TextBlock Language="{Binding Lang, Converter={StaticResource languageConverter}}">

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}}">
再见回来 2024-11-07 02:43:42

您可以创建附加属性并使用它。

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

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}" />
作死小能手 2024-11-07 02:43:42

语言可以绑定到 CultureInfo 实例或 string 区域性名称。

为避免错误

属性“Language”的绑定不能使用目标元素的语言进行转换;如果需要区域性,则必须在 Binding 上显式指定 ConverterCulture

根据请求在绑定集 ConverterCulture 上显式指定 ConverterCulture:(

<Setter Property="Language" Value="{Binding CurrentUICulture.Name, Source={x:Static threading:Thread.CurrentThread}, ConverterCulture={x:Static globalization:CultureInfo.InvariantCulture}}"/>

Source={x:Static threading:Thread.CurrentThread} 用作示例,并且应该如果语言绑定到 DataContext 中的属性,则删除)

Language can be bound to CultureInfo instance or string culture name.

To avoid error

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

set ConverterCulture for that binding as requested:

<Setter Property="Language" Value="{Binding CurrentUICulture.Name, Source={x:Static threading:Thread.CurrentThread}, ConverterCulture={x:Static globalization:CultureInfo.InvariantCulture}}"/>

( Source={x:Static threading:Thread.CurrentThread} is used as an example and should be removed if Language binds to property in the DataContext)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文