WPF - 无法删除 DataGrid 单元格周围的填充/边距

问题描述 投票:0回答:1

我尝试修改数据网格单元周围的白边

(我隐藏了单元格的 H/V 标题,也很抱歉隐藏单元格中的文本,这是公司财产相关的东西)

这是我定义单元格的方式,我注意到如果我注释 CustomComponent 则白色填充仍然存在,如果我注释 DataGridTemplateColumn 则白色填充消失

<DataGrid x:Name="modificationsGrid" ItemsSource="{Binding DisplayedTreeviewOperations}" PreviewMouseDown="PreviewMouseDown_OperationsDataGrid">
    <DataGrid.Columns>

        <DataGridTemplateColumn Width="auto">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>

                    <ItemsControl ItemsSource="{Binding Content}">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>

                                <custom:CustomComponent></custom:CustomComponent>

                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>

                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>

   </DataGrid.Columns>

这是我的自定义样式表:

<!-- Custom DataGrid -->
<Style TargetType="DataGrid">
    <Setter Property="AutoGenerateColumns" Value="False"/>
    <Setter Property="HorizontalGridLinesBrush" Value="White"/>
    <Setter Property="VerticalGridLinesBrush" Value="White"/>
    <!--disable first empty column, remove select all corner that flickers, remove unused header-->
    <Setter Property="HeadersVisibility" Value="None"/>
    <Setter Property="AutoGenerateColumns" Value="False"/>
    <Setter Property="ColumnHeaderHeight" Value="0" />
    <Setter Property="SelectionMode" Value="Single"/>
    <Setter Property="Background" Value="{DynamicResource OmpInvisibleBrush}"/>
    <!--{DynamicResource {x:Static SystemColors.ControlBrushKey}-->
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
    <Setter Property="BorderBrush" Value="Transparent"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="RowDetailsVisibilityMode" Value="VisibleWhenSelected"/>
    <Setter Property="ScrollViewer.CanContentScroll" Value="True"/>
    <Setter Property="ScrollViewer.PanningMode" Value="Both"/>
    <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGrid}">
                <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True" UseLayoutRounding="True">
                    <ScrollViewer x:Name="DG_ScrollViewer" Focusable="False" UseLayoutRounding="True">
                        <ScrollViewer.Template>
                            <ControlTemplate TargetType="{x:Type ScrollViewer}">
                                <Grid UseLayoutRounding="True">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="*"/>
                                        <ColumnDefinition Width="Auto"/>
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="*"/>
                                        <RowDefinition Height="Auto"/>
                                    </Grid.RowDefinitions>
                                    <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" CanContentScroll="{TemplateBinding CanContentScroll}" CanHorizontallyScroll="False" Grid.ColumnSpan="2" CanVerticallyScroll="False" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" Grid.Row="1" UseLayoutRounding="True"/>
                                    <ScrollBar x:Name="PART_VerticalScrollBar" Grid.Column="2" Maximum="{TemplateBinding ScrollableHeight}" Orientation="Vertical" Grid.Row="1" UseLayoutRounding="True" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportHeight}"/>
                                    <Grid Grid.Column="1" Grid.Row="2" UseLayoutRounding="True">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="{Binding NonFrozenColumnsViewportHorizontalOffset, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type DataGrid}}}"/>
                                            <ColumnDefinition Width="*"/>
                                        </Grid.ColumnDefinitions>
                                        <ScrollBar x:Name="PART_HorizontalScrollBar" Grid.Column="1" Maximum="{TemplateBinding ScrollableWidth}" Orientation="Horizontal" UseLayoutRounding="True" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportWidth}"/>
                                    </Grid>
                                </Grid>
                            </ControlTemplate>
                        </ScrollViewer.Template>
                        <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" UseLayoutRounding="True"/>
                    </ScrollViewer>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsGrouping" Value="True"/>
                <Condition Property="VirtualizingPanel.IsVirtualizingWhenGrouping" Value="False"/>
            </MultiTrigger.Conditions>
            <Setter Property="ScrollViewer.CanContentScroll" Value="False"/>
        </MultiTrigger>
    </Style.Triggers>
</Style>

enter image description here

c# wpf datagrid padding margin
1个回答
0
投票

试试这个:

<Style TargetType="{x:Type DataGrid}">
    <Setter Property="AlternatingRowBackground" Value="Yellow" />
    <Setter Property="RowBackground" Value="LightBlue" />
    <Setter Property="HeadersVisibility" Value="All" />
    <Setter Property="RowHeaderWidth" Value="10" />
</Style>

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.