在FullRow选择模式下禁用DataGrid当前单元格边框

Mic*_*eyn 48 wpf wpfdatagrid wpf-4.0

我在行选择模式中使用DataGrid(即SelectionUnit="FullRow").我只想删除当用户突出显示一行时为当前单元格放置的边框,以便进行真正的全行选择(并且不选择单元格级别).我不介意网格保持当前单元格的概念,我只想删除那个讨厌的当前单元格边界,可能是通过改变当前单元格的样式.最简单的方法是什么?

Fre*_*lad 99

您可以将BorderThicknessfor 设置为DataGridCell0

<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>
Run Code Online (Sandbox Code Playgroud)

  • @Michael Yanni:你在谈论`FocusVisualStyle`.要禁用它,在`CellStyle`中将其设置为null,如`<Setter Property ="FocusVisualStyle"Value ="{x:Null}"/>` (14认同)
  • 这实际上并没有阻止那些细胞聚焦.你只是没有看到它.因此,tab键看起来不正常,因为焦点在单元格中循环,而不是行.我添加了一个解决这个问题的答案. (3认同)
  • 如果用户使用箭头键,这将不起作用。虚线选择边框仍然出现在单元格中。 (2认同)

Mar*_*eIV 9

在这里看到另一个接近的答案,但它并没有摆脱Focus矩形.这是如何消除所有边界.

<DataGrid.Resources>
    <Style TargetType="{x:Type DataGridCell}">
        <Setter Property="BorderThickness" Value="0" />
        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
    </Style>
</DataGrid.Resources>
Run Code Online (Sandbox Code Playgroud)

另外,从技术上来说,那些单元格仍然可以获得焦点(你只是看不到它),为了使tab键前进到下一行而不是下一个单元格,我根据上面的内容定义了一个单元格样式,但也添加了以下...

<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>
Run Code Online (Sandbox Code Playgroud)

...然后我将其应用于除第一列定义之外的所有内容.这样Tab键进入下一行,而不是下一行.

然而,回到边界.如果你想隐藏它们但仍希望它们成为间距原因的布局的一部分,请将上面的内容更改为...

<DataGrid.Resources>
    <Style TargetType="{x:Type DataGridCell}">
        <Setter Property="BorderBrush" Value="Transparent" />
        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
    </Style>
</DataGrid.Resources>
Run Code Online (Sandbox Code Playgroud)

请享用!:)


小智 6

<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>
Run Code Online (Sandbox Code Playgroud)