如何取消DataGrid单元格选中边框?

4
2个回答

11

你看到的虚线框是单元格的 FocusedVisualStyle

你需要重写它,使其为空白。

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

  • visualStyle 直接设置在单元格上

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

<DataGrid.CellStyle>
   <Style TargetType="DataGridCell">
      <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
   </Style>
</DataGrid.CellStyle>

或者,如果你想遵循微软的模板指南:

<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>

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

  • 其他选项:可以通过ElementStyleEditingElementStyle来完成

这种方法有点麻烦,因为ElementStyleEditingElementStyle是在列(Column)上定义的,这意味着您必须编辑每个列的ElementStyleEditingElementStyle

但基本上,这是相同的操作:您通过在每个列上设置ElementStyle和/或EditingElementStyle来将FocusVisualStyle设置为null或上述定义的样式。


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>

请注意,如果将DataGridCell.Focusable设置为false,则使用上/下箭头键在数据网格中导航将无法正常工作。

3
请注意 - 如果将 DataGridCell.Focusable 设为 false,则在数据网格中使用向上/向下箭头键导航将无法使用。 - Brandon Cuff

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接