如何抑制DataGrid单元格选择边框?

use*_*528 4 .net wpf xaml

我已尝试设置边框样式,如此处所示在FullRow选择模式下禁用DataGrid当前单元格边框,但它没有完全做到这一点.使用鼠标选择时禁用单元格边框选择,但使用键盘进行选择时仍有虚线单元格边框.有什么建议?

Dav*_*vid 10

你看到的虚线框是细胞的 FocusedVisualStyle

你需要覆盖它,使其为空白.

这里有2个选项(其中一个必须是正确的,但因为我没时间尝试,我不知道哪个)

  • visualStyle直接在单元格上设置

这意味着你必须通过CellStyle属性设置它:

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

或者如果您想遵守MS的模板指南:

<DataGrid.Resources>

    <!--CellFocusVisual-->
    <Style x:Key="CellFocusVisual">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Border>
                        <Rectangle StrokeThickness="0" Stroke="#00000000" StrokeDashArray="1 2"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</DataGrid.Resources>

<DataGrid.CellStyle>
   <Style TargetType="DataGridCell">
      <Setter Property="FocusVisualStyle" Value="{StaticResource CellFocusVisual}"/>
   </Style>
</DataGrid.CellStyle>
Run Code Online (Sandbox Code Playgroud)

(通过这种方式,你可以看到它是如何完成的)

  • 其他选择:通过ElementStyle或者完成EditingElementStyle

这更是一个Hasle城那里的,因为ElementStyleEditingElementStyle在列定义,至极意味着你必须编辑每列的ElementStyleEditingElementStyle.

但基本上,这是一回事:您通过ElementStyle和/或EditingElementStyle每列将FocusVisualStyle设置为null或上面定义的样式


Gre*_*som 9

您可以将Focusable设置为False.

<DataGrid ...
      SelectionUnit="FullRow">
   <DataGrid.CellStyle>
      <Style TargetType="DataGridCell">
         <Setter Property="BorderThickness" Value="0"/>
         <Setter Property="Focusable" Value="False"/>
      </Style>
   </DataGrid.CellStyle>
   <!-- ... -->
</DataGrid>
Run Code Online (Sandbox Code Playgroud)

请注意,如果将DataGridCell.Focusable设为false,则使用向上/向下箭头键在数据网格中导航将不起作用.

  • 仅供参考 - 如果您使DataGridCell.Focusable为false,那么使用向上/向下箭头键在数据网格中导航将不起作用. (3认同)