C# WPF Datagrid 如何禁用特定列上的选择单元格

标签 c# wpf xaml datagrid selection

SO 上的全新功能和 WPF 数据网格的全新功能。

我一直在搜索有关 Datagrid 的所有已打开线程,但找不到我的问题的任何答案。

我正在尝试将数据显示到数据网格中。到目前为止,一切都很好。 我试图让用户选择数据网格中的单元格。到目前为止,一切都很好。 我想禁用特定列上的选择单元格。

查看链接中的图片。

如何禁用 Torque 列或 Average 列上的选择单元格,并让用户仅选择 Mes1 Mes2 Mes3 列上的单元格>

Example of the datagrid

最佳答案

如果您想阻止某些列中的单元格被选中,您可以使用 DataGridCell 样式,将这些单元格的 IsHitTestVisible 属性设置为 true:

<DataGrid x:Name="dataGrid1">
    <DataGrid.Resources>
        <Style TargetType="{x:Type DataGridCell}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Column.Header, RelativeSource={RelativeSource Self}}" Value="Torque">
                    <Setter Property="IsHitTestVisible" Value="False"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Column.Header, RelativeSource={RelativeSource Self}}" Value="Average">
                    <Setter Property="IsHitTestVisible" Value="False"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.Resources>
</DataGrid>

I am just playing with the code. But when I click on a cell in for example Column Mes 1, and then use keyboard to navigate, cells of column Torque and Average can be focused :-(

我想您必须将 IsEnabled 属性设置为 false 然后:

<DataTrigger Binding="{Binding Column.Header, RelativeSource={RelativeSource Self}}" Value="Torque">
    <Setter Property="IsHitTestVisible" Value="False"/>
    <Setter Property="IsTabStop" Value="False"/>
    <Setter Property="IsEnabled" Value="False"/>
</DataTrigger>

关于C# WPF Datagrid 如何禁用特定列上的选择单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44757277/

相关文章:

c# - 从 SQL Server 读取 - 需要从 CSV 读取

c# - 从其他 Xaml 文件绑定(bind)到 Usercontrol 中的元素

c# - 列中的按钮,获取它来自 Click 事件处理程序的行

c# - 编译错误 : 'x' conflicts with the declaration of 'y.x' ?

C# IAsyncResult 等待所有

c# - .Net 消息发布/订阅模式

c# - 将 Kinect 的彩色摄像头视频流保存为 .avi 视频

c# - 测试 WPF 应用程序

c# - 在 C# (WPF) 中,当 UI 线程中的数据立即更改时,是否会发生数据绑定(bind)?

xaml - 如何在 Xamarin Forms 中超链接部分标签文本?