我在行选择模式下使用 DataGrid(即SelectionUnit="FullRow"
)。我只是想在用户突出显示一行时删除当前单元格周围的边框,以便进行真正的全行选择(并且没有单元格级别选择)。我不介意网格保持当前单元格的概念,我只想删除那个讨厌的当前单元格边框,也许是通过更改当前单元格的样式。最简单的方法是什么?
7 回答
您可以将 for 设置BorderThickness
为DataGridCell
0
<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>
在这里看到另一个很接近的答案,但它并没有摆脱焦点矩形。这是消除所有边界的方法。
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="BorderThickness" Value="0" />
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
</Style>
</DataGrid.Resources>
此外,由于从技术上讲,这些单元格仍然会获得焦点(您只是看不到它),为了使 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>
...然后我将其应用于除第一列定义之外的所有内容。这样,tab 键前进到下一行,而不是下一个单元格。
然而,回到边界。如果您想隐藏它们但仍希望它们成为间距原因的布局的一部分,请将上面的内容更改为...
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="BorderBrush" Value="Transparent" />
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
</Style>
</DataGrid.Resources>
享受!:)
<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>
如果你不想弄乱 XAML 样式,你可以做这个简单的 hack。它不如 XAML 样式好用,但您可以尝试一下,看看它是否适合您。简单地单击单元格很好,但是如果您尝试拖动单元格,这不会在之后移除焦点(尽管我很确定您可以添加另一个案例来检查它)。
private void YourDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
YourDataGrid.Focus();
}
PS:不要忘记将事件处理程序添加到您DataGrid
的SelectionChanged
属性中。
如果您使用的是,xceed
DataGridControl
则将其设置NavigationBehavior
为RowOnly
<xcdg:DataGridControl NavigationBehavior="RowOnly" SelectionMode="Single" ....
如果您只想在单元格可编辑和选中时显示边框,您可以覆盖 DataGridCell 模板并为单元格 IsSelected 而不是 IsReadOnly 添加一个多重触发器。如果为列或 DataGrid 设置 IsReadOnly = true,则单元格不会显示边框
<ControlTemplate x:Key="MellowDataGridCellTemplate" TargetType="{x:Type DataGridCell}">
<Grid>
<ContentPresenter VerticalAlignment="Center" />
<Rectangle Name="FocusVisual" Stroke="White" StrokeThickness="1" Fill="Transparent" HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" IsHitTestVisible="false" Opacity="0" />
</Grid>
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsReadOnly" Value="False" />
<Condition Property="IsSelected" Value="True" />
</MultiTrigger.Conditions>
<Setter TargetName="FocusVisual" Property="Opacity" Value="1"/>
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
在样式中使用模板
<Style TargetType="{x:Type DataGridCell}" x:Key="MellowGridDataGridCell">
<Setter Property="Template" Value="{StaticResource MellowDataGridCellTemplate}" />
</Style>
并使用风格
<DataGrid CellStyle={StaticResource MellowGridDataGridCell >
...
</DataGrid>
真正的答案是:
<!-- put it in your cell style -->
<DataTrigger Binding="{Binding SelectionUnit, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Value="FullRow">
<Setter Property="BorderThickness" Value="0" />
</DataTrigger>
问题是关于仅在 FullRow 选择模式下禁用它,但其他答案提供了一种解决方案,即使在单元格选择模式下也可以完全禁用它。
回答一个非常古老的问题,但信不信由你 WPF 和 WinForms 现在仍在使用。