如何更改 DataGrid 选定的行颜色 WPF

我在我的 WPF 应用程序中创建了一个数据网格。我想更改选定的行颜色。我有以下代码


<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

                    xmlns:local="clr-namespace:navigationApp.Resources">


    <Style x:Key="DataGridColumnHeaderGripper" TargetType="Thumb">

        <Setter Property="Width" Value="18"/>

        <Setter Property="Background" Value="#252526"/>

        <Setter Property="Template">

            <Setter.Value>

                <ControlTemplate TargetType="{x:Type Thumb}">

                    <Border Padding="{TemplateBinding Padding}" Background="Transparent" BorderBrush="#3e3e45">

                        <Rectangle HorizontalAlignment="Center" Width="1" Fill="{TemplateBinding Background}"/>

                    </Border>

                </ControlTemplate>

            </Setter.Value>

        </Setter>

    </Style>


    <Style TargetType="{x:Type DataGridCell}">

        <Setter Property="Padding" Value="8,5" />

        <Setter Property="FocusVisualStyle" Value="{x:Null}" />

        <Setter Property="Template">

            <Setter.Value>

                <ControlTemplate TargetType="{x:Type DataGridCell}">

                    <Grid Background="Transparent">

                        <ContentPresenter

                            Margin="{TemplateBinding Padding}"

                            Content="{TemplateBinding ContentControl.Content}"

                            ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"

                            ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}"

                            SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />

                    </Grid>

                </ControlTemplate>

            </Setter.Value>

        </Setter>

    </Style>

当前,选定的行颜色为白色或透明。所以我看不到所选行的详细信息。


哔哔one
浏览 974回答 2
2回答

RISEBY

您可以使用触发器更改所选行的颜色。如datagridrow样式所示。选择行时,将颜色设置为父控件的背景属性(即 Grid 作为 TargetName)。<Style TargetType="{x:Type DataGridRow}">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <Setter Property="Template">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Setter.Value>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ControlTemplate TargetType="{x:Type DataGridRow}">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Grid x:Name="selectedRow">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <DataGridCellsPresenter&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ItemsPanel="{TemplateBinding ItemsControl.ItemsPanel}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Grid.Column="1" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <DataGridDetailsPresenter&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Visibility="{TemplateBinding DataGridRow.DetailsVisibility}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Grid.Column="1"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Grid.Row="1"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SelectiveScrollingGrid.SelectiveScrollingOrientation="Both" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <DataGridRowHeader&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Visibility="Visible"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Grid.RowSpan="2"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </SelectiveScrollingGrid>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Grid>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ControlTemplate.Triggers>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Trigger Property="IsSelected" Value="True">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Setter Property="Background" Value="{DynamicResource ApplicationAccentBrushSecondary}" TargetName="selectedRow" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Trigger>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ControlTemplate.Triggers>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ControlTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Setter.Value>&nbsp; &nbsp; &nbsp; &nbsp; </Setter>&nbsp; &nbsp; </Style>

慕的地8271018

在我的项目中,我做了如下:<Style TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource DataGridCell}"><Setter Property="Background" Value="{StaticResource DataGridCellBackgroundBrush}"/><Setter Property="BorderBrush" Value="{StaticResource DataGridBorderBrush}" /><Setter Property="BorderThickness" Value="0"/><Style.Triggers>&nbsp; &nbsp; <!--&nbsp; IsSelected&nbsp; -->&nbsp; &nbsp; <Trigger Property="IsSelected" Value="True">&nbsp; &nbsp; &nbsp; &nbsp; <Setter Property="Foreground" Value="{StaticResource BlackBrush}" />&nbsp; &nbsp; </Trigger>&nbsp; &nbsp; <MultiTrigger>&nbsp; &nbsp; &nbsp; &nbsp; <MultiTrigger.Conditions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Condition Property="Controls:DataGridCellHelper.IsCellOrRowHeader" Value="True" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Condition Property="IsSelected" Value="True" />&nbsp; &nbsp; &nbsp; &nbsp; </MultiTrigger.Conditions>&nbsp; &nbsp; &nbsp; &nbsp; <Setter Property="Background" Value="{StaticResource DataGridCellBackgroundBrush}" />&nbsp; &nbsp; &nbsp; &nbsp; <Setter Property="BorderBrush" Value="{StaticResource DataGridCellBackgroundBrush}" />&nbsp; &nbsp; </MultiTrigger>//others properties&nbsp;DataGridCellBackgroundBrush而DataGridBorderBrush在我的颜色预定
打开App,查看更多内容
随时随地看视频慕课网APP