Disable DataGrid current cell border in FullRow selection mode

独自空忆成欢 提交于 2019-11-30 01:44:05

You could set the BorderThickness for DataGridCell to 0

<DataGrid ...
          SelectionUnit="FullRow">
    <DataGrid.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="BorderThickness" Value="0"/>
            <!-- Update from comments.
                 Remove the focus indication for the selected cell -->
            <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
        </Style>
    </DataGrid.CellStyle>
    <!-- ... -->
</DataGrid>

Saw another answer here that was close, but it didn't get rid of the Focus rectangle. Here's how to obliterate all of the borders.

<DataGrid.Resources>
    <Style TargetType="{x:Type DataGridCell}">
        <Setter Property="BorderThickness" Value="0" />
        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
    </Style>
</DataGrid.Resources>

Also, since technically those cells still do get focus (you just don't see it), to make the tab key advance to the next row instead of the next cell, I define a cell style based on the above but which also adds the following...

<DataGrid.Resources>
    <Style x:Key="NoFocusDataGridCell" TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource {x:Type DataGridCell}}">
        <Setter Property="Focusable"        Value="False" />
        <Setter Property="IsTabStop"        Value="False" />
        <Setter Property="IsHitTestVisible" Value="False" />
    </Style>
</DataGrid.Resources>

...then I apply that to all but the first column definition. That way the tab key advances to the next row, not the next cell.

Back to the borders however. If you want to hide them but still want them to be part of the layout for spacing-reasons, change the above to this...

<DataGrid.Resources>
    <Style TargetType="{x:Type DataGridCell}">
        <Setter Property="BorderBrush" Value="Transparent" />
        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
    </Style>
</DataGrid.Resources>

Enjoy! :)

<Style x:Key="DataGrid" TargetType="DataGrid">
    <Setter Property="CellStyle">
        <Setter.Value>
            <Style TargetType="DataGridCell">
                <Setter Property="BorderThickness" Value="0"/>
                <Setter Property="Foreground" Value="{Binding Foreground, RelativeSource={RelativeSource TemplatedParent}}" />
                <Setter Property="Background" Value="{Binding Background, RelativeSource={RelativeSource TemplatedParent}}" />
            </Style>
        </Setter.Value>
    </Setter>
</Style>

If you want to show a border only when the cell is editable and selected you can override the DataGridCell template and add a multitrigger for when the cell IsSelected and not IsReadOnly. Then no border will be shown for cells if you set IsReadOnly = true for the column or DataGrid

<ControlTemplate x:Key="MellowDataGridCellTemplate" TargetType="{x:Type DataGridCell}">
    <Grid>
        <ContentPresenter VerticalAlignment="Center" />
        <Rectangle Name="FocusVisual" Stroke="White" StrokeThickness="1" Fill="Transparent" HorizontalAlignment="Stretch" 
                           VerticalAlignment="Stretch" IsHitTestVisible="false" Opacity="0" />

    </Grid>
    <ControlTemplate.Triggers>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsReadOnly" Value="False" />
                <Condition Property="IsSelected" Value="True" />
            </MultiTrigger.Conditions>
            <Setter TargetName="FocusVisual" Property="Opacity" Value="1"/>
        </MultiTrigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

Use the template in a style

<Style TargetType="{x:Type DataGridCell}" x:Key="MellowGridDataGridCell">
    <Setter Property="Template" Value="{StaticResource MellowDataGridCellTemplate}" />
</Style>

And use the style

<DataGrid CellStyle={StaticResource MellowGridDataGridCell >
    ...
</DataGrid>

If you're using the xceed DataGridControl then set the NavigationBehavior to RowOnly

<xcdg:DataGridControl NavigationBehavior="RowOnly" SelectionMode="Single"  ....
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!