【问题标题】:How to change DataGrid selected row color WPF如何更改 DataGrid 选定的行颜色 WPF
【发布时间】:2018-09-10 06:25:28
【问题描述】:

我在我的 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>

    <Style TargetType="{x:Type DataGridRow}">
        <Setter Property="Margin" Value="0"/>
        <Setter Property="HorizontalAlignment" Value="Stretch" />
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridRow}">
                    <Grid>
                        <Border x:Name="BorderOutline" BorderThickness="2,1,1,1" />
                        <Border x:Name="BorderInline" BorderThickness="0" />
                        <Grid Background="Black" Opacity="0" />
                        <SelectiveScrollingGrid>
                            <SelectiveScrollingGrid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </SelectiveScrollingGrid.ColumnDefinitions>
                            <SelectiveScrollingGrid.RowDefinitions>
                                <RowDefinition Height="*" />
                                <RowDefinition Height="Auto" />
                            </SelectiveScrollingGrid.RowDefinitions>
                            <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>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsMouseOver" Value="True" />
                                <Condition Property="IsSelected" Value="False"/>
                            </MultiTrigger.Conditions>
                            <Setter Property="BorderBrush" Value="{DynamicResource ApplicationAccentBrush}" TargetName="BorderOutline" />
                            <Setter Property="Opacity" Value="0.8" TargetName="BorderOutline" />
                            <Setter Property="Background" Value="{DynamicResource ApplicationAccentBrushSecondary}" TargetName="BorderInline" />
                            <Setter Property="Opacity" Value="0.2" TargetName="BorderInline" />
                        </MultiTrigger>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="BorderBrush" Value="{DynamicResource ApplicationAccentBrush}" TargetName="BorderOutline" />
                            <Setter Property="Background" Value="{DynamicResource ApplicationAccentBrushSecondary}" TargetName="BorderInline" />
                            <Setter Property="Opacity" Value="0.3" TargetName="BorderInline" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style TargetType="{x:Type DataGridColumnHeader}">
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Foreground" Value="White" />
        <Setter Property="Padding" Value="8,2,8,2"/>
        <Setter Property="Background" Value="#252526"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
                    <Grid>
                        <Border Name="HeaderBorder" Padding="{TemplateBinding Padding}" BorderThickness="1,1,1,1" BorderBrush="#3e3e45" Background="#252526">
                            <ContentPresenter
                                Name="HeaderContent"
                                Margin="0,0,0,1"
                                RecognizesAccessKey="True"
                                Content="{TemplateBinding ContentControl.Content}"
                                ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
                                ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}"
                                HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
                                VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}"
                                SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
                        </Border>
                        <!--<Thumb x:Name="PART_HeaderGripper" HorizontalAlignment="Right" Margin="0,0,-9,0" Style="{StaticResource DataGridColumnHeaderGripper}"/>-->
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter TargetName="HeaderBorder" Property="Background" Value="{DynamicResource ApplicationAccentBrushSecondary}"/>
                        </Trigger>
                        <Trigger Property="IsPressed" Value="true">
                            <Setter TargetName="HeaderBorder" Property="Background" Value="{DynamicResource ApplicationAccentBrush}"/>
                            <Setter TargetName="HeaderContent" Property="Margin" Value="1,1,0,0"/>
                        </Trigger>
                        <Trigger Property="Selector.IsSelected" Value="True">
                            <Setter Property="TextElement.Foreground" Value="White"/>
                        </Trigger>

                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

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

【问题讨论】:

    标签: c# wpf xaml datagrid resourcedictionary


    【解决方案1】:

    您可以使用触发器更改所选行的颜色。如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>
    

    【讨论】:

      【解决方案2】:

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

      <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 
      

      DataGridCellBackgroundBrushDataGridBorderBrush 是我预定义的颜色

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-22
        • 2013-10-17
        • 2015-12-26
        • 1970-01-01
        • 1970-01-01
        • 2019-02-16
        相关资源
        最近更新 更多