如何禁用 WPF Datagrid 中的编辑单元格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6766408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:59:07  来源:igfitidea点击:

How can I disable editing cells in a WPF Datagrid?

wpfdatagridwpfdatagridcells

提问by Pouyan

I'm constructing a datagrid in Windows Presentation Foundation, and I have a problem. When a user double-clicks on a cell in my datagrid, the cell goes into edit mode. I want to prevent that. Instead I want users to be able to select the full row - not edit values in it.

我正在 Windows Presentation Foundation 中构建数据网格,但遇到了问题。当用户双击我的数据网格中的一个单元格时,该单元格进入编辑模式。我想阻止这种情况。相反,我希望用户能够选择整行 - 而不是编辑其中的值。

How can I make it so that double-clicks select the full row instead of putting the clicked-on cell in edit mode?

如何使双击选择整行而不是将单击的单元格置于编辑模式?

回答by Leslie Davies

The WPF DataGridhas an IsReadOnlyproperty that you can set to Trueto ensure that users cannot edit your DataGrid's cells.

WPFDataGrid有一个IsReadOnly属性,您可以将其设置True为确保用户无法编辑您DataGrid的单元格。

You can also set this value for individual columns in your DataGridas needed.

您还可以DataGrid根据需要为您中的各个列设置此值。

回答by Stephen

The DataGrid has an XAML property IsReadOnlythat you can set to true:

DataGrid 有一个 XAML 属性IsReadOnly,您可以将其设置为true

<my:DataGrid
    IsReadOnly="True"
/>

回答by VivekDev

If you want to disable editing the entire grid, you can set IsReadOnly to true on the grid. If you want to disable user to add new rows, you set the property CanUserAddRows="False"

如果要禁用编辑整个网格,可以在网格上将 IsReadOnly 设置为 true。如果要禁止用户添加新行,请设置属性 CanUserAddRows="False"

<DataGrid IsReadOnly="True" CanUserAddRows="False" />

Further more you can set IsReadOnly on individual columns to disable editing.

此外,您可以在单个列上设置 IsReadOnly 以禁用编辑。

回答by Célia

I see users in comments wondering how to disable cell editing while allowing row deletion : I managed to do this by setting all columns individually to read only, instead of the DataGrid itself.

我在评论中看到用户想知道如何在允许行删除的同时禁用单元格编辑:我设法通过将所有列单独设置为只读而不是 DataGrid 本身来做到这一点。

<DataGrid IsReadOnly="False">
    <DataGrid.Columns>
        <DataGridTextColumn IsReadOnly="True"/>
        <DataGridTextColumn IsReadOnly="True"/>
    </DataGrid.Columns>
</DataGrid>