如何使用ContentStringFormat显示百分比值? [英] How to display percent-values using ContentStringFormat?
问题描述
我现在玩了几个小时,但没有成功(可能是因为我对WPF比较陌生):
我有一个DataGrid的DataContext绑定到一个DataTable。 DataGrid的大小是固定的,其目的是保存一个值表y(x)(即头部显示x值,相应的y值从剪贴板粘贴并显示在第一个DataGridRow中)。到现在为止还挺好。这些值被完美地粘贴(作为字符串赋值给dataTable.rows [0] [i],其中i = 0 ... n)并且很好地显示。
显示的是百分比值,我希望它们显示为:
0.18将变成18%
$ b $所以,我决定使用适用于所有DataGridCell对象的样式来处理这个问题:
<< ; Style TargetType ={x:Type Controls:DataGridCell}>
< Style.Setters>
< Setter Property =ContentStringFormatValue ={} {0:P}/>
< Setter Property =ForegroundValue =DarkGray/>
< Setter Property =BackgroundValue =Yellow/>
< /Style.Setters>
< / style>然后背景和前景颜色被采用罚款,但数字仍然显示为小数(即b
0.18仍然是0.18。
我该如何解决这个问题?
b joerg
解决方案我相信你可以指定StringFormat和datagrid列的绑定定义。 >
< DataGridTextColumn Header =Column_HeaderBinding ={Binding Path = Field_Name,StringFormat ='{} {0:P}'} />
希望这有助于您,问候
Greetings to the enlightened ones!
I'm playing on this for several hours now, but wasn't successful (perhaps because I'm quite new to WPF):
I have a DataGrid whose DataContext is bound to a DataTable. The DataGrid is of fixed size and its purpose is to hold a value table y(x) (i.e. the headers show the x-values and the corresponding y-values are pasted from the clipboard and shown in the first DataGridRow). So far so good. The values are pasted (assigned as strings to dataTable.rows[0][i] where i=0...n) perfectly and displayed well.
But the numbers displayed are percent-values and I want them to be displayed as such:
"0.18" shall become "18 %"
So, I decided to cope with this using a style which is to be applied to all DataGridCell objects:
<Style TargetType="{x:Type Controls:DataGridCell}">
<Style.Setters>
<Setter Property="ContentStringFormat" Value="{}{0:P}"/>
<Setter Property="Foreground" Value="DarkGray"/>
<Setter Property="Background" Value="Yellow"/>
</Style.Setters>
</Style>
Then the background and foreground colors are adopted fine, but the numbers are still displayed as decimals (i.e. "0.18" still reads "0.18".
How can I fix this?
Thanks in advance
Joerg
解决方案 I believe you can specify StringFormat along with binding definition for the datagrid column. Smth. like this:
<DataGridTextColumn Header="Column_Header" Binding="{Binding Path=Field_Name, StringFormat='{}{0:P}'}"/>
hope this helps, regards
这篇关于如何使用ContentStringFormat显示百分比值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!