【发布时间】:2011-07-25 00:44:26
【问题描述】:
我已经尝试按照Disable DataGrid current cell border in FullRow selection mode 的建议设置边框样式,但它并没有完全做到这一点。使用鼠标选择时禁用单元格边框选择,但使用键盘进行选择时仍然有虚线单元格边框。有什么建议吗?
【问题讨论】:
我已经尝试按照Disable DataGrid current cell border in FullRow selection mode 的建议设置边框样式,但它并没有完全做到这一点。使用鼠标选择时禁用单元格边框选择,但使用键盘进行选择时仍然有虚线单元格边框。有什么建议吗?
【问题讨论】:
您看到的虚线框是单元格的FocusedVisualStyle
您需要覆盖它,使其为空白。
这里有2个选项(其中一个必须是正确的,但由于我没有时间尝试,我不知道是哪个)
这意味着你必须通过CellStyle 属性来设置它:
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
</Style>
</DataGrid.CellStyle>
或者如果您想遵守 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>
(这样你就可以看到是怎么做的了)
ElementStyle 或EditingElementStyle 完成
这更麻烦,因为 ElementStyle 和 EditingElementStyle 是在列上定义的,这意味着您必须编辑每个列的 ElementStyle 和 EditingElementStyle。
但基本上,这是同一件事:您将 FocusVisualStyle 设置为 null 或通过每个列上的 ElementStyle 和/或 EditingElementStyle 定义的样式
【讨论】:
您可以将 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,则使用向上/向下箭头键在数据网格中导航将不起作用。
【讨论】: