我正在试图弄清楚如何使以下WPF DataGrid中的行不可聚焦。正如您所看到的,我尝试在DataGrid中添加<DataGrid.Resources>
部分,我在其中指定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>
部分:
<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>
答案 0 :(得分:6)
使用IsHitTestVisible = False
<DataGrid.Resources>
<Style x:Key="NoFocusColumStyle" TargetType="{x:Type DataGridCell}">
<Setter Property="IsHitTestVisible" Value="False"/>
</Style>
</DataGrid.Resources>
然后将样式应用于要限制焦点的任何列
<DataGridTextColumn CellStyle="{StaticResource NoFocusColumStyle}" ... />