wpf 在 FullRow 选择模式下禁用 DataGrid 当前单元格边框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4547370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disable DataGrid current cell border in FullRow selection mode
提问by Michael Goldshteyn
I am using a DataGrid in row selection mode (i.e., SelectionUnit="FullRow"
). I simply want to remove the border that is being placed around the current cell when the user highlights a row in order to have true full row selection (and no cell level selection). I don't mind the notion of the grid maintaining the current cell, I just want to remove that pesky current cell border, perhaps by changing the style of the current cell. What is the easiest way to do this?
我在行选择模式(即,SelectionUnit="FullRow"
)中使用 DataGrid 。我只是想在用户突出显示一行时删除当前单元格周围放置的边框,以便进行真正的全行选择(并且没有单元格级别选择)。我不介意网格维护当前单元格的概念,我只想删除那个讨厌的当前单元格边框,也许是通过更改当前单元格的样式。什么是最简单的方法来做到这一点?
回答by Fredrik Hedblad
You could set the BorderThickness
for DataGridCell
to 0
您可以将BorderThickness
for设置为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>
回答by Mark A. Donohoe
Saw another answer here that was close, but it didn't get rid of the Focus rectangle. Here's how to obliterate all of the borders.
在这里看到另一个接近的答案,但它没有摆脱 Focus 矩形。这是消除所有边界的方法。
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="BorderThickness" Value="0" />
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
</Style>
</DataGrid.Resources>
Also, since technically those cells still do get focus (you just don't see it), to make the tab key advance to the next row instead of the next cell, I define a cell style based on the above but which also adds the following...
此外,由于从技术上讲这些单元格仍然获得焦点(您只是看不到它),为了使 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>
...then I apply that to all but the first column definition. That way the tab key advances to the next row, not the next cell.
...然后我将其应用于除第一列定义之外的所有内容。这样,tab 键前进到下一行,而不是下一个单元格。
Back to the borders however. If you want to hide them but still want them to be part of the layout for spacing-reasons, change the above to this...
然而,回到边界。如果您想隐藏它们但仍希望它们成为间距原因的布局的一部分,请将上述更改为...
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="BorderBrush" Value="Transparent" />
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
</Style>
</DataGrid.Resources>
Enjoy! :)
享受!:)
回答by marius
<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>
回答by AxdorphCoder
If you want to show a border only when the cell is editable and selected you can override the DataGridCell template and add a multitrigger for when the cell IsSelected and not IsReadOnly. Then no border will be shown for cells if you set IsReadOnly = true for the column or DataGrid
如果您只想在单元格可编辑和选择时显示边框,您可以覆盖 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>
Use the template in a style
在样式中使用模板
<Style TargetType="{x:Type DataGridCell}" x:Key="MellowGridDataGridCell">
<Setter Property="Template" Value="{StaticResource MellowDataGridCellTemplate}" />
</Style>
And use the style
并使用样式
<DataGrid CellStyle={StaticResource MellowGridDataGridCell >
...
</DataGrid>
回答by Simon_Weaver
If you're using the xceed
DataGridControl
then set the NavigationBehavior
to RowOnly
如果您使用的是xceed
DataGridControl
然后设置NavigationBehavior
为RowOnly
<xcdg:DataGridControl NavigationBehavior="RowOnly" SelectionMode="Single" ....