RUDE

Как удалить белую область в Datagrid WPF

Я привязал, чтобы удалить эту белую область из строки сетки данных.

Белая область нежелательна из-за дизайнерских целей. Я хочу изменить цвет этого места или полностью удалить его из сетки данных.


23
1

Ответ:

Решено

Короткий путь

В вашем объекте DataGrid измените свойство HeadersVisibility на столбец

<DataGrid HeadersVisibility = "Column" ... \>

Долгий путь (больше стиля)

You have to apply new styling for `RowStyle` in `DataGrid`, This way allow you to change the color or customize the entire row header.

Сосредоточьтесь на DataGridRowHeader, удалите линию, чтобы она исчезла, измените свойство Background, чтобы сделать ее черной или нужного вам цвета.

<Style x:Key = "DataGridRowStyle1" TargetType = "{x:Type DataGridRow}">
    <Setter Property = "Background" Value = "{DynamicResource {x:Static Colors.Red}}"/>
    <Setter Property = "SnapsToDevicePixels" Value = "true"/>
    <Setter Property = "Validation.ErrorTemplate" Value = "{x:Null}"/>
    <Setter Property = "ValidationErrorTemplate">
        <Setter.Value>
            <ControlTemplate>
                <TextBlock Foreground = "Red" Margin = "2,0,0,0" Text = "!" VerticalAlignment = "Center"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property = "Template">
        <Setter.Value>
            <ControlTemplate TargetType = "{x:Type DataGridRow}">
                <Border x:Name = "DGR_Border" Background = "{TemplateBinding Background}" BorderThickness = "{TemplateBinding BorderThickness}" BorderBrush = "{TemplateBinding BorderBrush}" SnapsToDevicePixels = "True">
                    <SelectiveScrollingGrid>
                        <SelectiveScrollingGrid.ColumnDefinitions>
                            <ColumnDefinition Width = "Auto"/>
                            <ColumnDefinition Width = "*"/>
                        </SelectiveScrollingGrid.ColumnDefinitions>
                        <SelectiveScrollingGrid.RowDefinitions>
                            <RowDefinition Height = "*"/>
                            <RowDefinition Height = "Auto"/>
                        </SelectiveScrollingGrid.RowDefinitions>
                        <DataGridCellsPresenter Grid.Column = "1" ItemsPanel = "{TemplateBinding ItemsPanel}" SnapsToDevicePixels = "{TemplateBinding SnapsToDevicePixels}"/>
                        <DataGridDetailsPresenter Grid.Column = "1" Grid.Row = "1" SelectiveScrollingGrid.SelectiveScrollingOrientation = "{Binding AreRowDetailsFrozen, Converter = {x:Static DataGrid.RowDetailsScrollingConverter}, ConverterParameter = {x:Static SelectiveScrollingOrientation.Vertical}, RelativeSource = {RelativeSource AncestorType = {x:Type DataGrid}}}" Visibility = "{TemplateBinding DetailsVisibility}"/>
                        <!--HERE: Change the background color property for the color, Or remove the whole line to make it disapper-->
                        <DataGridRowHeader Grid.RowSpan = "2" Background = "Black" SelectiveScrollingGrid.SelectiveScrollingOrientation = "Vertical" Visibility = "{Binding HeadersVisibility, Converter = {x:Static DataGrid.HeadersVisibilityConverter}, ConverterParameter = {x:Static DataGridHeadersVisibility.Row}, RelativeSource = {RelativeSource AncestorType = {x:Type DataGrid}}}"/>
                    </SelectiveScrollingGrid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property = "IsNewItem" Value = "True">
            <Setter Property = "Margin" Value = "{Binding NewItemMargin, RelativeSource = {RelativeSource AncestorType = {x:Type DataGrid}}}"/>
        </Trigger>
    </Style.Triggers>
</Style>