wpf 从 NewItemRow 插入行后将焦点设置为 NewItemRow
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14148160/
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
Set focus to NewItemRow after row inserted from a NewItemRow
提问by Anthbs
I'm using the DevExpress XPF GridControl's NewItemRowfor inserting data to my database.
我使用的DevExpress XPFGridControl的NewItemRow将数据插入到我的数据库。
When I press enter in the last cell it enters the data and returns to the top row of the grid.
当我在最后一个单元格中按 Enter 键时,它会输入数据并返回到网格的顶行。
What I would like it to do is return to the NewItemRowso the user can start inserting the new record without needing to reselect the NewItemRowrow.
我希望它做的是返回到NewItemRow以便用户可以开始插入新记录而无需重新选择该NewItemRow行。
How i could set focus to NewItemRow after row inserted from a NewItemRow?
从 NewItemRow 插入行后,如何将焦点设置为 NewItemRow?
ps: I'm asking here rather than the DevExpress forums as here gets a much quicker response :)
ps:我在这里提问而不是在 DevExpress 论坛上提问,因为这里的回复要快得多:)
EDIT:
编辑:
Here is some code I have tried but it had exactly the same result:
这是我尝试过的一些代码,但结果完全相同:
void MVVMFriendlyDxgTableView_RowUpdated(object sender, DevExpress.Xpf.Grid.RowEventArgs e)
{
if (RowUpdatedCommand != null)
{
if (e.Row != null)
{
if (RowUpdatedCommand.CanExecute(e.Row))
{
RowUpdatedCommand.Execute(e.Row);
if (e.RowHandle == GridControl.NewItemRowHandle)
{
Dispatcher.BeginInvoke(new Action(() =>
{
SelectRow(GridControl.NewItemRowHandle);
}));
e.Handled = true;
}
}
}
}
}
回答by Anthbs
It turned out that the problem I was having was due to updating the databound ObservableCollection resulting in a refresh of the grid and the different row being selected. The second code posted by DJKRAZE would have worked but my calls to the database are Async and therefore didn't occur before the RowUpdated event finished. To solve this I overwrote the OnDataChanged method to select the NewItemRow after the data has updated.
结果证明,我遇到的问题是由于更新数据绑定 ObservableCollection 导致网格刷新并选择了不同的行。DJKRAZE 发布的第二个代码本来可以工作,但我对数据库的调用是异步的,因此在 RowUpdated 事件完成之前没有发生。为了解决这个问题,我重写了 OnDataChanged 方法以在数据更新后选择 NewItemRow。
Here is the resulting code that worked.
这是有效的结果代码。
protected override void OnDataChanged(bool rebuildVisibleColumns)
{
base.OnDataChanged(rebuildVisibleColumns);
Dispatcher.BeginInvoke(new Action(() =>
{
this.FocusedRowHandle = GridControl.NewItemRowHandle;
}), null);
}
Thanks for the help.
谢谢您的帮助。
回答by MethodMan
You can simply set the grid view's FoucusedRow property to a new row object instance obtained from RowEventArgs:
您可以简单地将网格视图的 FoucusedRow 属性设置为从 RowEventArgs 获得的新行对象实例:
private void MVVMFriendlyDxgTableView_RowUpdated(object sender, DevExpress.Xpf.Grid.RowEventArgs e)
{
if (e.RowHandle == GridControl.NewItemRowHandle)
{
Dispatcher.BeginInvoke(new Action(() => { viewSource.View.MoveCurrentToLast(); }));
Dispatcher.BeginInvoke(new Action(() => { gridView.FocusedRow = e.Row; }));
e.Handled = true;
}
}
if the Flickering occurs you can try this
如果发生闪烁,你可以试试这个
if (e.RowHandle == GridControl.NewItemRowHandle)
Dispatcher.BeginInvoke(new Action(() =>
{
MVVMFriendlyDxgTableView.FocusedRowHandle = GridControl.NewItemRowHandle;
}), null);
To Sort and ObservableCollection look at this example Sort a Collection / ObservableCollection
Sort 和 ObservableCollection 看这个例子 Sort a Collection / ObservableCollection

