如何禁用 WPF Datagrid 中的单元格编辑?
我正在 Windows Presentation Foundation 中构建数据网格,但遇到了问题。当用户双击数据网格中的单元格时,该单元格进入编辑模式。我想阻止这种情况发生。相反,我希望用户能够选择整行 - 而不是编辑其中的值。
我怎样才能做到双击选择整行而不是将单击的单元格置于编辑模式?
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.
How can I make it so that double-clicks select the full row instead of putting the clicked-on cell in edit mode?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
DataGrid 有一个 XAML 属性
IsReadOnly
,您可以将其设置为true
:The DataGrid has an XAML property
IsReadOnly
that you can set totrue
:我在评论中看到用户想知道如何在允许删除行的同时禁用单元格编辑:我设法通过将所有列单独设置为只读而不是 DataGrid 本身来做到这一点。
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.
如果要禁止编辑整个网格,可以在网格上将 IsReadOnly 设置为 true。
如果要禁止用户添加新行,请设置属性 CanUserAddRows="False"
此外,您可以在各个列上设置 IsReadOnly 以禁止编辑。
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"
Further more you can set IsReadOnly on individual columns to disable editing.
如果您绑定到
ObservableCollection
并且您的DataGrid
有AutoGenerateColumns="True"
因为您不想搞乱XAML 中的各个列定义,似乎您可以使用 System.ComponentModel 中的 ReadOnlyAttribute :If you're binding to an
ObservableCollection<T>
and yourDataGrid
hasAutoGenerateColumns="True"
because you don't want to mess with individual column definitions in your XAML, it seems that you can use theReadOnlyAttribute
fromSystem.ComponentModel
:WPF
DataGrid
有一个IsReadOnly
属性,您可以将其设置为True
以确保用户无法编辑您的DataGrid
' s 细胞。您还可以根据需要为
DataGrid
中的各个列设置此值。The WPF
DataGrid
has anIsReadOnly
property that you can set toTrue
to ensure that users cannot edit yourDataGrid
's cells.You can also set this value for individual columns in your
DataGrid
as needed.