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

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

问题描述

我正在尝试弄清楚如何使以下WPF DataGrid中的行不可聚焦。如您所见,我尝试将< DataGrid.Resources> 部分添加到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样式,因此在选择时不突出显示该行。这是我的< 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>


推荐答案

使用 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天全站免登陆