在 WPF 中使用其他设置设置区域性 [英] Set culture with additional settings in WPF

查看:26
本文介绍了在 WPF 中使用其他设置设置区域性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我当前的文化(具有自定义十进制符号)传递给 WPF,以便它根据我在 Windows 中的区域和语言设置显示绑定值.

我的研究总是以类似于

所以你需要一个自定义的 Binding 类:

public class CultureAwareBinding : 绑定{公共文化意识绑定(字符串路径):基地(路径){ConverterCulture = CultureInfo.CurrentCulture;}}

然后你必须在你的 XAML 中使用它:

 

之后您应该会看到所需的输出:

I'm trying to pass my current culture (that has a custom decimal symbol) to WPF, so that it will display bound values according to my region and language settings in windows.

My researches always ended up with the a solution similar to this, which passes the language tag, but not any additional settings (like the decimal symbol).

How can I force WPF to use the whole current culture and not only the default language settings?

Questions about possible a possible workaround:

Can I somehow pass the current culture to the default value converters used by WPF? Or maybe override them?

解决方案

There's couple options. Maybe the easiest one is to wrap the values you want to databind to screen and call ToString for them. For example, if you have:

    public decimal Value
    {
        get { return this.value; }
        set
        {
            if (value == this.value) return;
            this.value = value;
            OnPropertyChanged();
        }
    }

Wrap it inside your ViewModel like this:

    public decimal Value
    {
        get { return this.value; }
        set
        {
            if (value == this.value) return;
            this.value = value;
            OnPropertyChanged("ValueString");
        }
    }

    public string ValueString
    {
        get { return this.value.ToString(CultureInfo.CurrentCulture); }
    }

And bind your UI against this new property:

        <TextBlock x:Name="Result" Text="{Binding ValueString}" Grid.Row="0"/>

This way you will automatically get the formatting based on your computer's culture settings:

Another alternative is to use the method presented in here: https://stackoverflow.com/a/19796279/66988

So you need a custom Binding class:

public class CultureAwareBinding : Binding
{
    public CultureAwareBinding(string path)
        : base(path)
    {
        ConverterCulture = CultureInfo.CurrentCulture;
    }
}

And then you have to use that in your XAML:

        <TextBlock x:Name="Result" Text="{wpfApplication9:CultureAwareBinding Value}" Grid.Row="0"/>

After which you should see the desired output:

这篇关于在 WPF 中使用其他设置设置区域性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆