我看到很多关于如何在DataGrid中为Selected行设置样式的示例,例如:
我可以禁用选定的行样式吗?我不想要覆盖选择行更改的每一件事.只是不希望任何明显的变化.要比创建模板更简单.
要么..
禁用选择行,如果这更容易..但从浏览这个看似hacky的论坛
Son*_*oul 75
想出XAML摆脱选择风格..不理想,但足够接近..
<Style x:Key="CellStyle" TargetType="{x:Type DataGridCell}">
<Setter Property="Foreground" Value="Black" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{x:Null}" />
<Setter Property="BorderBrush" Value="{x:Null}" />
</Trigger>
</Style.Triggers>
</Style>
Run Code Online (Sandbox Code Playgroud)
Dan*_*ens 21
这对我有用:
<DataGrid>
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="DataGridCell.IsSelected" Value="True">
<Setter Property="BorderBrush">
<Setter.Value>
<SolidColorBrush Color="Transparent"/>
</Setter.Value>
</Setter>
<Setter Property="Foreground"
Value="{DynamicResource
{x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="Transparent"/>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
<!-- ... -->
</DataGrid>
Run Code Online (Sandbox Code Playgroud)
Bri*_*ink 17
我找到了另一种适合我情况的方法.我为所有单元格设置了这种样式,因为我不希望用户选择任何单元格.
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="IsHitTestVisible" Value="False"/>
</Style>
Run Code Online (Sandbox Code Playgroud)
我将首先回答第二个问题:要禁用行的选择,您可以更改DataGrid的RowStyle.
<DataGrid>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="IsEnabled" Value="False"/>
</Style>
</DataGrid.RowStyle>
<!--Other DataGrid items-->
</DataGrid>
Run Code Online (Sandbox Code Playgroud)
但是,这会更改文本样式,因为行本身现在已"禁用".它也没有否定用户仍然可以右键单击该行来选择它的事实.如果您真的想要禁用与datagrid行的任何类型的交互,您可以执行以下操作:
<DataGrid>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="IsHitTestVisible" Value="False"/>
</Style>
</DataGrid.RowStyle>
<!--Other DataGrid items-->
</DataGrid>
Run Code Online (Sandbox Code Playgroud)
由于行仍处于启用状态,因此文本样式不会更改.
现在,如果你想只改变所选行的风格,但先不谈功能,你可以做以下(这基本上是一样的@丹史蒂文斯的答案).该ControlTextBrushKey是由系统使用以彩色文本项目的画笔.请查看此答案以获取DynamicResource和StaticResource之间的解释.
<DataGrid>
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="DataGridCell.IsSelected" Value="True">
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="Background" Value="Transparent"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
<!--Other DataGrid items-->
</DataGrid>
Run Code Online (Sandbox Code Playgroud)
重要的是要注意,当选择行时,上述解决方案不会更改DataGridRowHeader的样式,如下所示(选择第一行).
这是相对简单的:
datagrid.SelectionChanged += (obj, e) =>
Dispatcher.BeginInvoke(DispatcherPriority.Render, new Action(() =>
datagrid.UnselectAll()));
Run Code Online (Sandbox Code Playgroud)
这将禁用DataGrid上的所有选择。
如果您不想完全禁用选择,而只是将其隐藏,则需要修改模板。
| 归档时间: |
|
| 查看次数: |
46715 次 |
| 最近记录: |