C#WPF - DataGrid中的背景颜色

问题描述 投票:-1回答:2

我是WPF的新手,我尝试使用自动生成的列创建DataGrid。现在我按照自己的意愿对DataGrid进行了风格化,但是当DG小于ParentContent时会出现问题,看起来像这样。

enter image description here

我不喜欢它看起来是DG右侧的白色条,但我不知道如何改变它的颜色。我希望它看起来像在DataGridView中。

enter image description here

我的问题是,如何更改网格部分的颜色?

网格代码

<DataGrid x:Class="Project.WPF.Grid"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:Project.WPF"
      mc:Ignorable="d" 
      d:DesignHeight="50" d:DesignWidth="200"
      Name="dgv_grid"
      Height="Auto" Width="Auto"
      AutoGenerateColumns="True" 
      SelectionMode="Single"
      ItemsSource="{Binding}" 
      DataContext="{Binding}"
      Background="#ABABAB" 
      SelectionUnit="FullRow"
      CanUserAddRows="False"
      CanUserReorderColumns="False"
      CanUserResizeColumns="True"
      CanUserResizeRows="False"
      IsReadOnly="True"
      GridLinesVisibility="None"
      HorizontalGridLinesBrush="#A0A0A0"
      VerticalGridLinesBrush="#A0A0A0"
      BorderThickness="1"
      BorderBrush="#636363"
      EnableColumnVirtualization="True"
      RowHeaderWidth="0"
      ColumnWidth="Auto"
      Loaded="dgv_grid_Loaded" 
      AutoGeneratedColumns="dgv_grid_AutoGeneratedColumns"
      AutoGeneratingColumn="dgv_grid_AutoGeneratingColumn"
      MouseDoubleClick="dgv_grid_MouseDoubleClick"
      SelectedCellsChanged="dgv_grid_SelectedCellsChanged">

<DataGrid.ColumnHeaderStyle>
    <Style TargetType="{x:Type DataGridColumnHeader}">
        <!--#region Triggers-->
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="BorderBrush" Value="#A0A0A0"/>
                <Setter Property="BorderThickness" Value="1,1,1,1"/>
            </Trigger>
            <Trigger Property="IsPressed" Value="True">
                <Setter Property="BorderBrush" Value="#A0A0A0"/>
                <Setter Property="BorderThickness" Value="1,1,1,1"/>
                <Setter Property="Background" Value="#FFB6B6B6"/>
            </Trigger>
        </Style.Triggers>
        <!--#endregion-->
        <!--#region Setters-->
        <!--<Setter Property="Height" Value="Auto"/>-->
        <Setter Property="FontFamily" Value="Myanmar Text"/>
        <Setter Property="FontSize" Value="12"/>
        <Setter Property="FontWeight" Value="SemiBold"/>
        <Setter Property="Background" Value="#ffffff"/>
        <Setter Property="BorderThickness" Value="1,1,0,1"/>
        <Setter Property="Padding" Value="7,5,5,0"/>
        <Setter Property="BorderBrush" Value="#e5e5e5"/>
        <Setter Property="TextBlock.TextAlignment" Value="Left"/>
        <Setter Property="VerticalAlignment" Value="Stretch"/>
        <Setter Property="TextBlock.TextWrapping" Value="WrapWithOverflow"/>
        <!--#endregion-->
    </Style>
</DataGrid.ColumnHeaderStyle>

<DataGrid.RowHeaderStyle>
    <Style TargetType="{x:Type DataGridRowHeader}">

        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="BorderBrush" Value="#A0A0A0"/>
                <Setter Property="BorderThickness" Value="1,1,1,1"/>
            </Trigger>
            <Trigger Property="IsPressed" Value="True">
                <Setter Property="BorderBrush" Value="#A0A0A0"/>
                <Setter Property="BorderThickness" Value="1,1,1,1"/>
                <Setter Property="Background" Value="#FFB6B6B6"/>
            </Trigger>
        </Style.Triggers>

        <Setter Property="Background" Value="#ffffff"/>
        <Setter Property="BorderBrush" Value="#e5e5e5"/>
        <Setter Property="BorderThickness" Value="1,1,1,0"/>
        <Setter Property="Width" Value="33"/>
        <Setter Property="Height" Value="20"/>
    </Style>
</DataGrid.RowHeaderStyle>

<DataGrid.CellStyle>
    <Style TargetType="{x:Type DataGridCell}">
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="#FF7B7E99"/>
                <Setter Property="BorderThickness" Value="0"/>
            </Trigger>
            <Trigger Property="IsKeyboardFocusWithin" Value="True">
                <Setter Property="Background" Value="#FF4DA2D8"/>
                <Setter Property="BorderThickness" Value="0"/>
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Background" Value="#ABABAB"/>
            </Trigger>
        </Style.Triggers>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridCell}">
                    <Grid Background="{TemplateBinding Background}">
                        <Border BorderBrush="#a0a0a0" BorderThickness="1,1,0,0"/>
                        <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" Margin="6,2,6,2"/>
                        <ContentControl FontFamily="Myanmar Text" FontSize="20"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</DataGrid.CellStyle>

c# wpf datagrid background border
2个回答
0
投票

如果您阅读MSDN的ControlTemplate for DataGrid:

DataGrid Styles and Templates

你会看到大量的代码。我不知道您是否熟悉ControlTemplates。但是这个代码背后的ScrollViewer上有一个ColumnDefinition :(在MSDN中我的意思)

<ScrollViewer x:Name="DG_ScrollViewer" Focusable="false" Background="Black">
     <ScrollViewer.Template>
            <ControlTemplate TargetType="{x:Type ScrollViewer}">
                 <Grid>
                     <Grid.ColumnDefinitions>
                     <ColumnDefinition Width="Auto" />
                     <ColumnDefinition Width="*" />
                     <ColumnDefinition Width="Auto" />

您可以看到此定义将填充父容器。因为中间列的宽度是*,它将填充整个父级。

我不知道如何在WPF默认值中删除此列。但是如果你有时间设置DataGrid的样式。您可以更改MSDN的ControlTemplate并删除此行:

 <ColumnDefinition Width="*" />

所以你会得到DataGridView给你的东西。


0
投票

如果你不想让DataGrid水平拉伸,你可以将它的HorizontalAlignment属性设置为Left

<DataGrid x:Class="Project.WPF.Grid" ... HorizontalAlignment="Left" />

如果你想改变空白部分的背景,你可以将DataGrid放在Grid中并设置这个的Background属性:'

<Grid Background="Gray">
    <DataGrid ... HorizontalAlignment="Left" />
</Grid>
© www.soinside.com 2019 - 2024. All rights reserved.