WPF数据网格选中行样式

46
我遇到了一个非常愚蠢的问题 - 需要在WPF DataGrid中为选定的行设置样式。
我想显示一个带有蓝色边框的矩形,而不仅仅是填充整行的某种颜色。
有任何实现此功能的想法吗?一定有一些很简单的方法。
3个回答

102

使用CellStyleRowStyle来设置DataGrid的样式。DataGridCellDataGridRow都有IsSelected属性,可以在Trigger中使用它们来查找是否已选中。

类似以下代码即可实现:

<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>

只需不断尝试,直到做对为止。


2
边框的渲染效果正如我所期望的那样,但是我无法处理背景/前景颜色,在选择一行时,它会填充为纯蓝色。 - illegal-immigrant
我不明白。你是想说 BackgroundForeground 颜色没有应用吗?你能发一下你使用的代码吗?你使用的是哪个版本的 WPF? - decyclone
2
我似乎遇到了和Illegal-Immigrant一样的问题;似乎无法覆盖默认的蓝色行背景颜色,只能更改边框颜色。 - Steve Smith
我使用 *,2* 等设置列宽,以便列恰好填充网格的宽度。如果我在 RowStyle 中设置 BorderThickness=2,则会导致数据网格出现水平滚动条。作为替代方案,我将 BorderThickness 设置为 "0,1,0,1",这样它只会在行上方和下方显示边框。 - Phil Jollans

10

我喜欢这个:

<Style TargetType="{x:Type DataGridRow}">
    <Setter Property="BorderBrush" Value="LightGray" />
    <Setter Property="BorderThickness" Value="1" />
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="BorderBrush" Value="Blue" />
        </Trigger>
    </Style.Triggers>
    <Style.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
    </Style.Resources>
</Style>

6

试试这个

<DataGrid.Resources>
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>

    <Style TargetType="{x:Type DataGridRow}">
        <Setter Property="HeaderStyle">
            <Setter.Value>
                <Style TargetType="{x:Type DataGridRowHeader}">
                    <Setter Property="Visibility" Value="Collapsed"/>
                    <Setter Property="Width" Value="0"/>
                </Style>
            </Setter.Value>
        </Setter>
        <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
        <Setter Property="SnapsToDevicePixels" Value="true"/>
        <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>
        <Setter Property="ValidationErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <TextBlock Foreground="Red" Margin="2,0,0,0" Text="!" VerticalAlignment="Center"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridRow}">
                    <Border x:Name="DGR_Border" BorderThickness="1" CornerRadius="5" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                        <SelectiveScrollingGrid>
                            <SelectiveScrollingGrid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                            </SelectiveScrollingGrid.ColumnDefinitions>
                            <Grid Grid.Column="1">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="*"/>
                                    <RowDefinition Height="Auto"/>
                                </Grid.RowDefinitions>
                                <DataGridCellsPresenter ItemsPanel="{TemplateBinding ItemsPanel}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                <DataGridDetailsPresenter Margin="4" Grid.Row="1" SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding AreRowDetailsFrozen, ConverterParameter={x:Static SelectiveScrollingOrientation.Vertical}, Converter={x:Static DataGrid.RowDetailsScrollingConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Visibility="{TemplateBinding DetailsVisibility}"/>

                                <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Grid.RowSpan="2"/>
                            </Grid>
                            <DataGridRowHeader SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Row}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
                        </SelectiveScrollingGrid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter TargetName="DGR_Border" Property="BorderBrush" Value="Blue"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="DetailsVisibility" Value="Visible">
                <Setter Property="BorderThickness" Value="4,1,4,4"/>
                <Setter Property="BorderBrush" Value="#FF3886B9"/>
            </Trigger>
            <!--<Trigger Property="IsSelected" Value="True">
                <Setter Property="BorderBrush" Value="Blue"/>
            </Trigger>-->
        </Style.Triggers>
    </Style>

</DataGrid.Resources>

我尝试了这个解决方案,它有效,但是它引入了另一个问题。我在一行中有一个超链接单元格。如果我将此setter应用于IsSelected = true,则需要单击两次超链接。有没有办法避免这种情况? - deathrace

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接