Silverlight 忽略CurrencyGroupSeparator

发布于 2024-11-02 00:20:50 字数 506 浏览 3 评论 0原文

Thread.CurrentThread.CurrentCulture = New CultureInfo("sv-SE")
Thread.CurrentThread.CurrentUICulture = New CultureInfo("sv-SE")

Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyGroupSeparator = " "
Thread.CurrentThread.CurrentUICulture.NumberFormat.CurrencyGroupSeparator = " "

我正在尝试这样做:

   <TextBlock Text={Binding decimalValue, StringFormat=c2}/>

它正确设置文化并添加“kr”(瑞典货币符号)。但是,不尊重组分隔符设置。即使我将其设置为“-”或其他任何它也不起作用。

大问号?漏洞?

Thread.CurrentThread.CurrentCulture = New CultureInfo("sv-SE")
Thread.CurrentThread.CurrentUICulture = New CultureInfo("sv-SE")

Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyGroupSeparator = " "
Thread.CurrentThread.CurrentUICulture.NumberFormat.CurrencyGroupSeparator = " "

Im trying to do:

   <TextBlock Text={Binding decimalValue, StringFormat=c2}/>

It sets the culture properly and adds the "kr" which is the swedish currency symbol. However, doesnt honor the group separator setting. Even if I set it to "-" or whatever it doesnt work.

Big questionmark? Bug?

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

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

发布评论

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

评论(1

秋凉 2024-11-09 00:20:50

我不确定您是否能够解决这个问题,但由于没有人回答,我会的。
对于初学者来说,我不确定您的支持代码是否与表示层在同一线程中运行;可能不是,我相信 Silverlight 创建了自己的视觉线程。换句话说,设置线程范围的 CultureInfo 不会解决您的问题。

有(至少)两种方法可以解决此问题:
1. 使用 StringFormat 属性来设置自定义格式。
2. 在支持代码中创建动态属性,该属性将为您格式化值。请找到这个不完美的示例:

public decimal Quote { get; set; }

// Formats value of Quote property
public string FormattedQuote
{
    get
    {
        CultureInfo swedishCulture = new CultureInfo("sv-SE");
        swedishCulture.NumberFormat.CurrencyGroupSeparator = " ";
        return Quote.ToString("c2", swedishCulture);
    }
}

并且在您的 XAML 代码中,您不需要指定格式,因此您只需这样做:

<TextBlock Name="textBlock1" Text="{Binding FormattedQuote}" DataContext="{Binding ElementName=textBlock1}" />

I am not sure if you were able to work around this problem, but since nobody answered, I will.
For starters, I am not sure if your backing code runs in the same thread as presentation layer; probably not, that is I believe Silverlight creates its own visual thread. In other words setting thread-wide CultureInfo will not resolve your problem.

There are (at least) two ways to resolve this issue:
1. Play with StringFormat attribute to set custom format.
2. Create dynamic property in the backing code which will format the value for you. Please find this imperfect example:

public decimal Quote { get; set; }

// Formats value of Quote property
public string FormattedQuote
{
    get
    {
        CultureInfo swedishCulture = new CultureInfo("sv-SE");
        swedishCulture.NumberFormat.CurrencyGroupSeparator = " ";
        return Quote.ToString("c2", swedishCulture);
    }
}

And in your XAML code, you do not need specify format, so you would only do this:

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