基于 WPF DataGrid 中的 DataGridTemplateColumn 设置列样式
我正在使用 WPF DataGrid,其中如果该行可编辑,其中一列需要显示“编辑”超链接 - 这由该行的支持模型中的布尔标志指示。我能够使用 DataGridTemplateColumn 实现这一点 - 没有问题。然而,对整行的附加要求是在选择该行时不显示任何突出显示(默认情况下为蓝色背景)。我已经能够通过定义具有透明背景的 DataGridCell 样式在其他列上实现此目的,例如,
<DataGridTextColumn
Header="Id"
Binding="{Binding Path=Id}"
HeaderStyle="{StaticResource DataGridColumnHeaderStyle}"
CellStyle="{StaticResource DataGridCellStyle}" />
其中 DataGridCellStyle 定义如下:
<Style x:Key="DataGridCellStyle" TargetType="{x:Type DataGridCell}">
<Setter Property="Background" Value="Transparent" />
...
</Style>
但是,有问题的列 DataGridTemplateColumn 不提供我可以使用的“CellStyle”属性关闭选择突出显示。所以我的问题是使用 DataGridTemplateColumn 时如何设置单元格样式?这是我对满足第一个要求的列的实现(即,如果该行可编辑,则显示“编辑”超链接):
<DataGridTemplateColumn
Header="Actions"
HeaderStyle="{StaticResource CenterAlignedColumnHeaderStyle}">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock
Visibility="{Binding Path=Editable, Converter={StaticResource convVisibility}}"
Style="{StaticResource CenterAlignedElementStyle}">
<Hyperlink
Command="..."
CommandParameter="{Binding}">
<TextBlock Text="Edit" />
</Hyperlink>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
谢谢。
I am using a WPF DataGrid where one of the columns has a requirement to show an "Edit" hyperlink if the row is editable - this is indicated by a boolean flag in the backing model for the row. I was able to achieve this using a DataGridTemplateColumn - no problems. However an additional requirement on the entire row is not to show any highlights when the row is selected (this is a blue background by default). I have been able to achieve this on other columns by defining the DataGridCell style with a transparent background, e.g.
<DataGridTextColumn
Header="Id"
Binding="{Binding Path=Id}"
HeaderStyle="{StaticResource DataGridColumnHeaderStyle}"
CellStyle="{StaticResource DataGridCellStyle}" />
where DataGridCellStyle is defined as follows:
<Style x:Key="DataGridCellStyle" TargetType="{x:Type DataGridCell}">
<Setter Property="Background" Value="Transparent" />
...
</Style>
However the column in question, a DataGridTemplateColumn, does not offer a "CellStyle" attribute which I can use for turning off selection highlights. So my question is how to set the cell style when using a DataGridTemplateColumn? Here's my implementation of the column which satisfies the first requirement (i.e. showing an "Edit" hyperlink if the row is editable):
<DataGridTemplateColumn
Header="Actions"
HeaderStyle="{StaticResource CenterAlignedColumnHeaderStyle}">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock
Visibility="{Binding Path=Editable, Converter={StaticResource convVisibility}}"
Style="{StaticResource CenterAlignedElementStyle}">
<Hyperlink
Command="..."
CommandParameter="{Binding}">
<TextBlock Text="Edit" />
</Hyperlink>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
至少在WPF4中,有一个DataGridTemplateColumns的CellStyle:http://msdn。 microsoft.com/en-us/library/cc189163.aspx
At least in WPF4, there is a CellStyle for DataGridTemplateColumns: http://msdn.microsoft.com/en-us/library/cc189163.aspx