使 WPF 数据网格中的行不可聚焦
我试图弄清楚如何使以下 WPF DataGrid 中的行不可聚焦。正如您所看到的,我尝试将
部分添加到 DataGrid,在其中指定 DataGrid 单元格样式,但这不起作用。我缺少什么?
<DataGrid Name="grdResources"
AutoGenerateColumns="False" SelectionUnit="FullRow"
AlternatingRowBackground="LightBlue" CanUserDeleteRows="False" CanUserAddRows="False"
CanUserReorderColumns="False" ClipboardCopyMode="ExcludeHeader">
<DataGrid.Resources>
<DataGridTemplateColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Focusable" Value="False"/>
</Style>
</DataGridTemplateColumn.CellStyle>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTemplateColumn Header="Select" IsReadOnly="True" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Name="Select" Tag="{Binding}" Click="Select_Click"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Key" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<Label Content="{Binding Path=Key}"></Label>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Value" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<TextBlock TextWrapping="Wrap" Text="{Binding Path=Value}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
编辑 对于那些感兴趣的人,我最终覆盖了 SelectedRow 样式,因此它在选择时不会突出显示该行。这是更改后我的
部分:
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="BorderThickness" Value="1"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
I'm trying to figure out how to make the rows in the following WPF DataGrid non-focusable. As you can see I tried adding a <DataGrid.Resources>
section to the DataGrid where I'm specifying a DataGrid cell style but this isn't working. What am I missing?
<DataGrid Name="grdResources"
AutoGenerateColumns="False" SelectionUnit="FullRow"
AlternatingRowBackground="LightBlue" CanUserDeleteRows="False" CanUserAddRows="False"
CanUserReorderColumns="False" ClipboardCopyMode="ExcludeHeader">
<DataGrid.Resources>
<DataGridTemplateColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Focusable" Value="False"/>
</Style>
</DataGridTemplateColumn.CellStyle>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTemplateColumn Header="Select" IsReadOnly="True" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Name="Select" Tag="{Binding}" Click="Select_Click"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Key" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<Label Content="{Binding Path=Key}"></Label>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Value" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<TextBlock TextWrapping="Wrap" Text="{Binding Path=Value}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
EDIT
For those interested I ended up overriding the SelectedRow style so it doesn't highlight the row when selected. Here is my <DataGrid.Resources>
section after that change:
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="BorderThickness" Value="1"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
IsHitTestVisible = False
然后将样式应用于您想要限制焦点的任何列
Use
IsHitTestVisible = False
Then apply the style to whatever columns you want to restrict focus for