C#WPF-数据网格外的背景色

C#WPF-数据网格外的背景色,c#,wpf,datagrid,background,border,C#,Wpf,Datagrid,Background,Border,我是WPF新手,我尝试用自动生成的列创建DataGrid。 现在我得到了我想要的DataGrid样式,但是当DG小于ParentContent时会出现一个问题,看起来是这样的 我不喜欢DG右侧的白色条的外观,但我不知道如何改变它的颜色。我希望它看起来像DataGridView中的一样 我的问题是,如何更改网格的该部分的颜色 网格代码 <DataGrid x:Class="Project.WPF.Grid" xmlns="http://schemas.microsoft.co

我是WPF新手,我尝试用自动生成的列创建DataGrid。 现在我得到了我想要的DataGrid样式,但是当DG小于ParentContent时会出现一个问题,看起来是这样的

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

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

网格代码

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


如果您阅读MSDN的DataGrid控制模板:

您将看到大量代码。 我不知道你是否熟悉ControlTemplates。但在ScrollViewer上,在这些代码后面有一个列定义: (在MSDN中我指的是)


因此,您将获得DataGridView提供给您的功能。

如果您不希望
DataGrid
水平拉伸,可以将其
水平对齐
属性设置为

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

我尝试了第一种,但没有达到DGV的效果。关于第二个,它可能是一个很好的解决方案,但这是一个Winforms应用程序中的托管控件,我在类中编写了许多行,如
公共类CustomGrid:DataGrid
重写所有代码不是我现在想要的,所以我不认为现在将控件嵌套在网格中是解决方案:(我真的不明白链接中写了多少代码,但是删除行
columndefinition
会对我的应用程序产生什么影响,因为如果我理解得很好,你说我必须更新WPF DataGrid模板的源代码。
 <ColumnDefinition Width="*" />
<DataGrid x:Class="Project.WPF.Grid" ... HorizontalAlignment="Left" />
<Grid Background="Gray">
    <DataGrid ... HorizontalAlignment="Left" />
</Grid>