如何在 WPF 用户控件中显示动态区域性格式的数字

发布于 2024-12-01 00:22:04 字数 1102 浏览 0 评论 0原文

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

我在 asp.NET MVC 中使用扩展方法做了类似的事情,但需要帮助来了解如何将其组合在一起在 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>

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.

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

Example MVC Extension Method

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

Window

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

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

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

发布评论

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

评论(2

以往的大感动 2024-12-08 00:22:04

如果我正确理解你的问题,你想绑定特定 TextBlock 的区域性。

您无法绑定 Binding 的属性,因此绑定 ConverterCulture 将不起作用。

FrameworkElement 上有一个 Language 属性,可以很好地设置,

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

但是,当尝试绑定此属性时,我遇到了一个奇怪的异常
我可能会自己就这个异常提出一个问题

属性“语言”的绑定不能使用目标元素的
转换语言;如果需要文化,ConverterCulture
必须在绑定上显式指定。

根据 Thomas Levesque 的回答,这应该是可能的,所以也许我做错了什么.. WPF xml:lang/Language 绑定

我所做的只是使用附加行为,该行为又会更新MyCulture 更新时的语言

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

语言行为

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());
    }
}

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

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

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

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.

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

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());
    }
}
妳是的陽光 2024-12-08 00:22:04

看来转换器就是答案。该界面包含文化价值观。

    Convert(object value, Type targetType, object parameter, CultureInfo culture) 

但我找不到传递文化的语法。
抱歉,这不是完整且经过测试的答案,但我没时间了。

绑定文化的 URL。
http://msdn.microsoft.com/en -us/library/system.windows.data.binding.converterculture.aspx

传递参数的语法是:

    Converter={StaticResource colorConverter}, ConverterParameter=GREEN}" 

您可能需要使用 ConverterParameter 将区域性作为字符串传递。

我同意 Meleak 的观点,不能将参数绑定到转换器。给了他+1。
但我认为你可以用 MultiBinding 转换器来愚弄它。

    <TextBlock Name="textBox2" DataContext="{StaticResource NameListData}">
        <TextBlock.Text>
            <MultiBinding Converter="{StaticResource myCutlureConverter}"
              ConverterParameter="FormatLastFirst">
                  <Binding Path="InputValue"/>
                  <Binding Path="CultureTxt"/>
            </MultiBinding>
         </TextBlock.Text>
    </TextBlock>

It seems like a converter is the answer. The interface includes a values for culture.

    Convert(object value, Type targetType, object parameter, CultureInfo culture) 

But I could not find syntax for passing culture.
Sorry this is not a full and tested answer but I ran out of time.

URL on binding culture.
http://msdn.microsoft.com/en-us/library/system.windows.data.binding.converterculture.aspx

The syntax for passing a a parameter is:

    Converter={StaticResource colorConverter}, ConverterParameter=GREEN}" 

You may need to pass culture as a string using ConverterParameter.

I agree with Meleak that cannot bind the parameter to a converter. Gave him a +1.
But I think you can fool it with a MultiBinding converter.

    <TextBlock Name="textBox2" DataContext="{StaticResource NameListData}">
        <TextBlock.Text>
            <MultiBinding Converter="{StaticResource myCutlureConverter}"
              ConverterParameter="FormatLastFirst">
                  <Binding Path="InputValue"/>
                  <Binding Path="CultureTxt"/>
            </MultiBinding>
         </TextBlock.Text>
    </TextBlock>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文