在 WPF 数据网格中使行不可聚焦 [英] Making a row non-focusable in a WPF datagrid

查看:24
本文介绍了在 WPF 数据网格中使行不可聚焦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚如何使以下 WPF DataGrid 中的行不可聚焦.如您所见,我尝试将 部分添加到 DataGrid 中,我在其中指定了 DataGrid 单元格样式,但这不起作用.我错过了什么?

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>

编辑对于那些感兴趣的人,我最终覆盖了 SelectedRow 样式,因此它在选择时不会突出显示该行.这是更改后的 部分:

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>

推荐答案

使用 IsHitTestVisible = False

<DataGrid.Resources>
    <Style x:Key="NoFocusColumStyle" TargetType="{x:Type DataGridCell}">
        <Setter Property="IsHitTestVisible" Value="False"/>
    </Style>
</DataGrid.Resources>

然后将样式应用于要限制焦点的任何列

Then apply the style to whatever columns you want to restrict focus for

<DataGridTextColumn CellStyle="{StaticResource NoFocusColumStyle}" ... />

这篇关于在 WPF 数据网格中使行不可聚焦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆