在WPF DataGrid中基于DataGridTemplateColumn的样式列 [英] Styling columns based on DataGridTemplateColumn in a WPF DataGrid

查看:990
本文介绍了在WPF DataGrid中基于DataGridTemplateColumn的样式列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用WPF DataGrid,其中的一列要求如果该行是可编辑的,则显示编辑超链接-这由该行的支持模型中的布尔标志指示。我能够使用DataGridTemplateColumn实现此目的-没问题。但是,整行的另一项要求是在选择该行时不显示任何突出显示(默认为蓝色背景)。通过在透明背景下定义DataGridCell样式,我能够在其他列上实现此目标,例如

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}" />

其中DataGridCellStyle的定义如下:

where DataGridCellStyle is defined as follows:

<Style x:Key="DataGridCellStyle" TargetType="{x:Type DataGridCell}">
    <Setter Property="Background" Value="Transparent" />
    ...
</Style>

但是有问题的列DataGridTemplateColumn没有提供我可以使用的 CellStyle属性用于关闭选择突出显示。所以我的问题是使用DataGridTemplateColumn时如何设置单元格样式?这是我满足第一个要求的列的实现(即,如果行可编辑,则显示编辑超链接):

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>

谢谢。

推荐答案

至少在WPF4中,有一个用于DataGridTemplateColumns的CellStyle: http://msdn.microsoft.com/zh-CN/library/cc189163.aspx

At least in WPF4, there is a CellStyle for DataGridTemplateColumns: http://msdn.microsoft.com/en-us/library/cc189163.aspx

这篇关于在WPF DataGrid中基于DataGridTemplateColumn的样式列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆