我想从自定义转换器设置属性 ContentStringFormat 将DataGrid中的一些小数格式化为百分比。:
<Setter Property="ContentStringFormat">
<Setter.Value>
<MultiBinding Converter="{StaticResource PercentageConverter}">
<Binding Path="ItemsSource" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}" />
<Binding RelativeSource="{RelativeSource Self}" />
<Binding />
</MultiBinding>
</Setter.Value>
我设法添加条件,以便格式仅适用于某些单元格。但是......我不知道该返回什么,我已经尝试了return {0:P2}
但它没有效果:
class PercentageConverter
...
public object Convert(...)
if (myCustomConditions)
{
return "{0:P2}" // how to return my format??
}
我需要返回什么来设置ContentStringFormat?非常感谢您的帮助!
答案 0 :(得分:0)
最后,我操纵了绑定源,并以我想要的格式从双打切换到字符串。为了排序,我实现了一个自定义排序器,将字符串转换回双打。
丑陋,但对我来说足够漂亮。
答案 1 :(得分:-3)
或者你可以将值乘以100并在结尾处附加%符号并在转换中除以100 ..
Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.Convert
If TypeOf value Is Double OrElse TypeOf value Is Decimal Then
Return value * 100
Else
Return 0
End If
End Function
Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
If TypeOf value Is Double OrElse TypeOf value Is Decimal Then
Return value / 100
Else
Return 0
End If
End Function