如何在 WPF 用户控件中显示动态区域性格式的数字
我想动态设置数字文本块的区域性格式,并将区域性和数字值传递到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我正确理解你的问题,你想绑定特定
TextBlock
的区域性。您无法绑定
Binding
的属性,因此绑定ConverterCulture
将不起作用。FrameworkElement
上有一个Language
属性,可以很好地设置,但是,当尝试绑定此属性时,我遇到了一个奇怪的异常
我可能会自己就这个异常提出一个问题
根据 Thomas Levesque 的回答,这应该是可能的,所以也许我做错了什么.. WPF xml:lang/Language 绑定
我所做的只是使用附加行为,该行为又会更新MyCulture 更新时的
语言
。语言行为
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 bindingConverterCulture
won't work.There is a
Language
property onFrameworkElement
which works fine to set like thisHowever, when trying to bind this property I get a weird exception
I'm probably going to ask a question on this exception myself
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.LanguageBehavior
看来转换器就是答案。该界面包含文化价值观。
但我找不到传递文化的语法。
抱歉,这不是完整且经过测试的答案,但我没时间了。
绑定文化的 URL。
http://msdn.microsoft.com/en -us/library/system.windows.data.binding.converterculture.aspx
传递参数的语法是:
您可能需要使用 ConverterParameter 将区域性作为字符串传递。
我同意 Meleak 的观点,不能将参数绑定到转换器。给了他+1。
但我认为你可以用 MultiBinding 转换器来愚弄它。
It seems like a converter is the answer. The interface includes a values for 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:
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.