快速(我希望如此)提问。我有一个DataGrid,其中包含几个货币列。我似乎找不到如何以2位小数显示货币的方法。但要保留单元格中可能存在的3或4位小数值。我尝试了一些字符串格式化,但那实际上会改变值。当然,IValueConverter也会这样做。那么我错过了什么?或者有人能指点我正确的方向吗?
编辑: 根据建议,我尝试了类似于以下内容的东西。
<Style x:Key="DecimalTextBlockStyle" TargetType="{x:Type DataGridCell}">
<Setter Property="TextBlock.Text" Value="{Binding StringFormat={}{0:C}}"/>
<Setter Property="TextBlock.Foreground" Value="Red"/>
<Setter Property="Background" Value="AliceBlue"/>
</Style>
并在此应用:
if(e.PropertyType.FullName.Contains("Decimal"))
{
var cl = e.Column as DataGridTextColumn;
if (cl != null)
{
//cl.Binding.StringFormat = "C2";
Style Stl = (Style)FindResource("DecimalTextBlockStyle");
cl.CellStyle = Stl;
}}
但是我不明白的是为什么那些列的数值都是漂亮的红色文本,蓝色背景......但是未格式化的字符串只显示0,000而不是€0,00。