WPF DataGridCell 中的条件格式
我正在尝试在 WPF DataGrid 上应用一些条件格式。要求如下。对于网格上的任何单元格,如果内容是整数,则显示格式应为零小数位。如果内容有小数位,则将其显示为字符串格式中定义的默认值。
有人知道我如何实现这种条件格式吗?我编写了一个转换器,可以检查小数是否存在,但我一直无法弄清楚如何在我的 XAML 中将其应用到我的单元格样式或我的文本列。
单元格样式 - 这是我想要定义的样式
<Style x:Key="MyCellStyle" TargetType="DataGridCell">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
</Style>
网格列 - 我将应用应用于此列的样式
<DataGridTextColumn x:Key="ColumnName"
Header="ColumnName"
SortMemberPath="MyColumnSort"
Binding="{Binding myColBinding, StringFormat={}{0:N2}}"
IsReadOnly="True"
Width="40" />
转换器 - 我将使用此转换器来确定是否应用此样式。
public class NoDecimalConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
int converted;
int.TryParse(value.ToString(), out converted);
value.ToString();
return int.TryParse(value.ToString(), out converted);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
我尝试通过向单元格样式添加数据触发器来实现此目的,如下所示,但出现错误,指出“无法在“System.Windows.Controls.DataGridCell”类型上找到样式属性“StringFormat”。”
<Style x:Key="MyCellStyle" TargetType="DataGridCell">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Style.Triggers>
<DataTrigger Binding="{Binding NoDecimalConvertor}" Value="False" >
<Setter Property="StringFormat" Value="true" />
</DataTrigger>
</Style.Triggers>
</Style>
几天来我一直在努力反对这个问题,所以任何关于解决这个问题的最佳方法的指导将不胜感激。
I am trying to apply some conditional formatting on a WPF DataGrid.The requirement is as follows. For any cell on the grid, if the content is an integer, the display format should be zero decimal places. If the contents have decimal places, then display them to the default value defined in the stringformat.
Does anybody have any idea as to how I could achieve this kind of conditional formatting? I have written a converter which can check for the existence of decimals but I have been unable to work out how I would be able to apply this in my XAML to either my cell style or my text column.
Cell Style - This is the style that I would like to define
<Style x:Key="MyCellStyle" TargetType="DataGridCell">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
</Style>
Grid Column - I would to apply the style applied to this column
<DataGridTextColumn x:Key="ColumnName"
Header="ColumnName"
SortMemberPath="MyColumnSort"
Binding="{Binding myColBinding, StringFormat={}{0:N2}}"
IsReadOnly="True"
Width="40" />
Converter - I would use this converter to determine whether this style gets applied.
public class NoDecimalConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
int converted;
int.TryParse(value.ToString(), out converted);
value.ToString();
return int.TryParse(value.ToString(), out converted);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
I tried to do it by adding a datatrigger to my cell style as below but I got an error stating "Cannot find the Style Property 'StringFormat' on the type 'System.Windows.Controls.DataGridCell'."
<Style x:Key="MyCellStyle" TargetType="DataGridCell">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Style.Triggers>
<DataTrigger Binding="{Binding NoDecimalConvertor}" Value="False" >
<Setter Property="StringFormat" Value="true" />
</DataTrigger>
</Style.Triggers>
</Style>
I have been banging my head against this for days so any guidance would be appreciated as to the best way to approach this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的转换器当前返回一个布尔值,以指示它是否可以从字符串值中解析整数。相反,您应该尝试这样的操作:
这将首先尝试解析 int,如果成功则返回它(作为字符串)。如果没有,它将尝试 double 并返回它 - 在这里您可以指定任何字符串格式来获取所需的小数位数等。最后,如果它无法解析,它将返回原始值。
然后,您可以使用以下方法在列上进行设置:
注意:您需要使用
MyConverter
键在资源中的某个位置实例化转换器。如果您希望能够指定默认的小数位数,可以将其作为转换器参数传递。
Your converter is currently returning a boolean to indicate whether or not it could parse an integer from the string value. Instead, you should try something like this:
This will first try to parse an int, and return it (as a string) if successful. If not, it will try a double and return it - here you could specify any string format to get the desired number of decimal places etc. Finally, if it can't parse either it will return the original value.
You can then set that on your column using:
Note: you will need to instantiate your converter somewhere in the resources with a key of
MyConverter
.If you want to be able to specify a default number of decimal places, you could pass it in as a converter parameter.