WPF DataGrid - 以编程方式将单元格设置为编辑模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1629311/
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 21:08:04  来源:igfitidea点击:

WPF DataGrid - Set a cell into edit mode programmatically

wpfdatagrid

提问by Nir

  • I have a WPF DataGrid that shows some data records (bounded to an ObservableCollection).

  • When the user clicks "Edit" button, the currend selected row should move into edit-mode (As if the user double-clicked this row).

  • 我有一个 WPF DataGrid 显示一些数据记录(绑定到 ObservableCollection)。

  • 当用户单击“编辑”按钮时,当前选定的行应进入编辑模式(就像用户双击该行一样)。

How do I do that?

我怎么做?

回答by alkk

Assuming WPF:

假设 WPF:

<DataGrid x:Name="dg".... />

Then this code will work:

然后此代码将起作用:

dg.CurrentCell = new DataGridCellInfo(dg.Items[i], dg.Columns[j]);
dg.BeginEdit();

回答by Heinzi

Here is the documentation of the WPF DataGrid on MSDN. The BeginEditmethod seems to be what you are looking for.

这是 MSDN 上 WPF DataGrid 的文档。该BeginEdit方法似乎是你在找什么。

PS: I don't know if this is suitable for your application, but many DataGrid users find Single-Click Editinguseful.

PS:我不知道这是否适合您的应用程序,但许多 DataGrid 用户发现单击编辑很有用。