我尝试了这里建议的设置边框样式(禁用FullRow选择模式下DataGrid当前单元格的边框),但它并没有完全达到效果。当您使用鼠标进行选择时,它会禁用单元格边框选择,但是当您使用键盘进行选择时仍然存在虚线单元格边框。有什么建议吗?
你看到的虚线框是单元格的 FocusedVisualStyle
。
你需要重写它,使其为空白。
这里有两个选项(其中一个必须是正确的,但由于我没有时间尝试,我不知道哪一个):
这意味着你必须通过 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>
(通过这种方式,您可以看到它是如何完成的)
ElementStyle
或EditingElementStyle
来完成这种方法有点麻烦,因为ElementStyle
和EditingElementStyle
是在列(Column)上定义的,这意味着您必须编辑每个列的ElementStyle
和EditingElementStyle
。
但基本上,这是相同的操作:您通过在每个列上设置ElementStyle
和/或EditingElementStyle
来将FocusVisualStyle设置为null或上述定义的样式。
<DataGrid ...
SelectionUnit="FullRow">
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Focusable" Value="False"/>
</Style>
</DataGrid.CellStyle>
<!-- ... -->
</DataGrid>