gpt4 book ai didi

c# - XAML 中的文化感知货币格式

转载 作者:行者123 更新时间:2023-11-30 23:26:17 26 4
gpt4 key购买 nike

我想格式化绑定(bind)到数据网格中的单元格/列的货币值。

相关XAML如下:

<telerik:RadGridView Grid.Row="0" Grid.ColumnSpan="2" x:Name="dataGrid" 
AutoGenerateColumns="False" HorizontalAlignment="Stretch"
VerticalAlignment="Top" Margin="0,25,0,0" IsSearchingDeferred="True"
IsReadOnly="True" IsLocalizationLanguageRespected="False">
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn DataMemberBinding="{Binding Path=VALUATION, StringFormat=C}"
Header="{x:Static properties:Resources.AddressLine3}"/>
</telerik:RadGridView.Columns>
</telerik:RadGridView>

这是当前绑定(bind)到列的值(该值作为数字文字来自数据库):

12345678.90

这是文化设置为 en-US 时输出的值

12345678.90 //notice the decimal point

这是文化设置为nl-NL时输出的值

12345678,90 //notice the comma

你可以看到文化在某种程度上得到了尊重。但是,输出不是我所期望的。

对于 en-US 区域性,我希望输出为 $12,345,678.90。对于 nl-NL 文化,我希望输出为 € 12.345.680,00

在我后面的 XAML 代码中使用以下代码手动设置区域性(用于测试目的):

public MainWindow()
{
//I am toggling between the following cultures
CultureInfo ci = new CultureInfo("nl-NL");
//CultureInfo ci = new CultureInfo("ja-JP");
//CultureInfo ci = new CultureInfo("en-US");

//I am forcing the UI to use the culture I specify
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
this.Language = XmlLanguage.GetLanguage(ci.Name);
Properties.Resources.Culture = ci;

//these are used to test that to string formatting works as expected
//when using the debugger
float price = 12345678.90F;
string currencyToDisplay = price.ToString("C");

InitializeComponent();
}

问题

即使我已将格式指定为货币,为什么格式不显示货币符号和数字分隔符?

在后面的代码中手动格式化相同的数字会得到我想要的结果,但我想在 XAML 中执行此操作。

我尝试过的我已尝试以下解决方法无济于事:

<telerik:GridViewDataColumn DataMemberBinding="{Binding Path=VALUATION, StringFormat=C}" Header="{x:Static properties:Resources.AddressLine3}"/>
<telerik:GridViewDataColumn DataMemberBinding="{Binding Path=VALUATION, StringFormat={}{0:C}}" Header="{x:Static properties:Resources.AddressLine3}"/>
<telerik:GridViewDataColumn DataMemberBinding="{Binding Path=VALUATION, StringFormat=\{0:C\}}" Header="{x:Static properties:Resources.AddressLine3}"/>
<telerik:GridViewDataColumn DataMemberBinding="{Binding Path=VALUATION, StringFormat='C'}" Header="{x:Static properties:Resources.AddressLine3}"/>
<telerik:GridViewDataColumn DataMemberBinding="{Binding Path=VALUATION, StringFormat=c}" Header="{x:Static properties:Resources.AddressLine3}"/>
<telerik:GridViewDataColumn DataMemberBinding="{Binding VALUATION}" DataFormatString="{}{0:C}" Header="{x:Static properties:Resources.AddressLine3}"/>

有什么我遗漏的吗?

最佳答案

正如我们在评论中所说的那样。
不显示货币符号的可能原因是您使用的 UserControl 没有使用正确的区域性。当我使用 DynamicResource 设置 Button 的样式而不在 xaml 中包含 ResourceDictionary 时,我发现了类似的行为。我的意思是部分应用了样式,例如在您的情况下点变成了逗号但没有货币符号。
强制 UserCpntrol 使用所需区域性的一种方法是将此代码段包含在您的 UserControl xaml 文件中:

<UserControl x:Class="WpfApplication1.Views.CultureExplicitUC"
xml:lang="en-GB"<!--This is what you want in your UC-->
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApplication1.Views"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>

</Grid>
</UserControl>

现在对于转换器,您需要创建如下所示的类:

namespace WpfApplication1.Converters
{
public class DebugConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
//manipulate your data here
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}

请注意,转换器有一个 cultureInfo 作为参数,因此如果需要,您可以根据开发环境手动更改它。
此外,通过使用转换器,您可以通过以下简单检查来确定传入的数据类型:
如果(值为整数)

关于c# - XAML 中的文化感知货币格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36919561/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com