如何在WPF用户控件中显示动态区域性格式的数字 [英] How to display dynamic culture formatted number in a WPF UserControl

查看:117
本文介绍了如何在WPF用户控件中显示动态区域性格式的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过传递到MyUserControl的区域性和数字值来动态设置Number文本块的区域性格式。 MyCulture和Number值将传递给MyCustomControl,格式为 en-GB, en-US等。

I would like to dynamically set the culture format of the Number textblock with culture and number values passed through to MyUserControl. The MyCulture and Number values are passed to MyCustomControl and will be of the form "en-GB", "en-US" etc.

我在asp中做了类似的操作。 NET MVC具有扩展方法,但需要帮助如何在WPF中将其组合在一起。

I did something similar in asp.NET MVC with an extension method but need help for how to piece this together in WPF.

示例MVC扩展方法

public static MvcHtmlString CulturedAmount(this decimal value, 
    string format, string locale)
{
    if (string.IsNullOrEmpty(locale))
        locale = HttpContext.Current.Request.UserLanguages[0];

    return MvcHtmlString.Create(value.ToString(format, 
        CultureInfo.CreateSpecificCulture(locale)));
}

窗口

//MyMoney is a decimal, MyCulture is a string (e.g. "en-US")
<MyCustomControl Number="{Binding MyMoney}" Culture="{Binding MyCulture}" 
    Text="Some Text" />

MyCustomControl

<StackPanel>
    <TextBlock Text="{Binding Number, ElementName=BoxPanelElement, 
        StringFormat={}{0:C}}" /> //display this with specific culture
    <TextBlock Text="{Binding Text, ElementName=BoxPanelElement}" />
</StackPanel>


推荐答案

如果我正确理解了您的问题,则希望将特定 TextBlock 的区域性。

If I understand your question correctly you want to bind the culture for a specific TextBlock.

您不能绑定 Binding的属性,因此绑定 ConverterCulture 无效。

You can't bind the properties of a Binding so binding ConverterCulture won't work.

有一个 FrameworkElement 上的语言属性,可以像这样设置很好

There is a Language property on FrameworkElement which works fine to set like this

<TextBlock Language="en-US"
           Text="{Binding Number,
                          ElementName=BoxPanelElement,
                          StringFormat={}{0:C}}"/>

但是,当尝试绑定此属性时,出现奇怪的异常

我可能会问自己一个关于此异常的问题

However, when trying to bind this property I get a weird exception
I'm probably going to ask a question on this exception myself


绑定属性 Language不能使用目标元素的
转换语言;如果需要文化,则必须在绑定上明确指定ConverterCulture

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.

根据托马斯·莱维斯克这应该是可能的,但也许我做错了。。 WPF xml:lang /语言绑定

According to this answer by Thomas Levesque this should be possible though so maybe I did something wrong.. WPF xml:lang/Language binding

我所做的所有工作都是使用一种附加行为当更新MyCulture时,依次更新语言

All I got working was using an attached behavior which in turn updated Language when MyCulture updated.

<TextBlock local:LanguageBehavior.Language="{Binding MyCulture}"
           Text="{Binding MyNumber,
                          ElementName=BoxPanelElement,
                          StringFormat={}{0:C}}"/>

LanguageBehavior

public class LanguageBehavior
{
    public static DependencyProperty LanguageProperty =
        DependencyProperty.RegisterAttached("Language",
                                            typeof(string),
                                            typeof(LanguageBehavior),
                                            new UIPropertyMetadata(LanguageBehavior.OnLanguageChanged));

    public static void SetLanguage(FrameworkElement target, string value)
    {
        target.SetValue(LanguageBehavior.LanguageProperty, value);
    }
    public static string GetLanguage(FrameworkElement target)
    {
        return (string)target.GetValue(LanguageBehavior.LanguageProperty);
    }
    private static void OnLanguageChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
    {
        FrameworkElement element = target as FrameworkElement;
        element.Language = XmlLanguage.GetLanguage(e.NewValue.ToString());
    }
}

这篇关于如何在WPF用户控件中显示动态区域性格式的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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