简体   繁体   English

XAML中的DataGrid行和单元格样式

[英]DataGrid Row & Cell Style in XAML

I want to set a DataGrid style within app.xaml . 我想在app.xaml设置一个DataGrid样式。 I've tried adding a style in however I am unsure of the semantics required to add a Cell and Row style. 我尝试添加样式,但是不确定添加单元格和行样式所需的语义。

This is what I have tried so far; 到目前为止,这是我尝试过的。

<Style TargetType="DataGrid" x:Name="noighlightRowDataGrid">
    <DataGrid.CellStyle>
        <Style TargetType="DataGridCell">
            <Style.Triggers>
                <Trigger Property="IsSelected"
                Value="True">
                    <Setter Property="Background"
                Value="White" />
                    <Setter Property="Foreground"
                Value="Black" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </DataGrid.CellStyle>
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Style.Triggers>
                <Trigger Property="IsSelected"
                Value="True">
                    <Setter Property="BorderBrush"
                Value="Blue" />
                    <Setter Property="BorderThickness" Value="2" />
            </Trigger>
        </Style.Triggers>
    </Style>
</DataGrid.RowStyle>

I can see that dropping the DataGrid.CellStyle into the DataGrid style isn't going to work but like I said I am unsure on how to create the style properly. 我可以看到将DataGrid.CellStyle放到DataGrid样式中是行不通的,但是就像我说的那样,我不确定如何正确创建样式。

You can specify the row and cell styles independently. 您可以独立指定行样式和单元格样式。

<Style TargetType="DataGridCell">
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="White" />
            <Setter Property="Foreground" Value="Black" />
        </Trigger>
    </Style.Triggers>
</Style>
<Style TargetType="DataGridRow">
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="BorderBrush" Value="Blue" />
            <Setter Property="BorderThickness" Value="2" />
        </Trigger>
    </Style.Triggers>
</Style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM