为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
哔哔one 2022-01-09 17:06:57
我在我的 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>当前,选定的行颜色为白色或透明。所以我看不到所选行的详细信息。
查看完整描述

2 回答

?
RISEBY

TA贡献1856条经验 获得超5个赞

您可以使用触发器更改所选行的颜色。如datagridrow样式所示。选择行时,将颜色设置为父控件的背景属性(即 Grid 作为 TargetName)。


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

        <Setter Property="Template">

            <Setter.Value>

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

                    <Grid x:Name="selectedRow">                        

                            <DataGridCellsPresenter

                                ItemsPanel="{TemplateBinding ItemsControl.ItemsPanel}"

                                SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"

                                Grid.Column="1" />

                            <DataGridDetailsPresenter

                                Visibility="{TemplateBinding DataGridRow.DetailsVisibility}"

                                Grid.Column="1"

                                Grid.Row="1"

                                SelectiveScrollingGrid.SelectiveScrollingOrientation="Both" />

                            <DataGridRowHeader

                                Visibility="Visible"

                                Grid.RowSpan="2"

                                SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical" />

                        </SelectiveScrollingGrid>

                    </Grid>

                    <ControlTemplate.Triggers>                                                        

                        <Trigger Property="IsSelected" Value="True">

                            <Setter Property="Background" Value="{DynamicResource ApplicationAccentBrushSecondary}" TargetName="selectedRow" />

                        </Trigger>

                    </ControlTemplate.Triggers>

                </ControlTemplate>

            </Setter.Value>

        </Setter>

    </Style>


查看完整回答
反对 回复 2022-01-09
?
慕的地8271018

TA贡献1796条经验 获得超4个赞

在我的项目中,我做了如下:


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

    <!--  IsSelected  -->

    <Trigger Property="IsSelected" Value="True">

        <Setter Property="Foreground" Value="{StaticResource BlackBrush}" />

    </Trigger>

    <MultiTrigger>

        <MultiTrigger.Conditions>

            <Condition Property="Controls:DataGridCellHelper.IsCellOrRowHeader" Value="True" />

            <Condition Property="IsSelected" Value="True" />

        </MultiTrigger.Conditions>

        <Setter Property="Background" Value="{StaticResource DataGridCellBackgroundBrush}" />

        <Setter Property="BorderBrush" Value="{StaticResource DataGridCellBackgroundBrush}" />

    </MultiTrigger>

//others properties 

DataGridCellBackgroundBrush而DataGridBorderBrush在我的颜色预定


查看完整回答
反对 回复 2022-01-09
  • 2 回答
  • 0 关注
  • 973 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号