Styling columns based on DataGridTemplateColumn in a WPF DataGrid

风格不统一 提交于 2019-12-10 19:55:35

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/2709305/styling-columns-based-on-datagridtemplatecolumn-in-a-wpf-datagrid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!