添加新行并编辑第一个单元格数据网格wpf(我要放弃)紧急 [英] Add new Row and edit first cell datagrid wpf(Shall i give up) URGENT

查看:55
本文介绍了添加新行并编辑第一个单元格数据网格wpf(我要放弃)紧急的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了各种各样的事情,但无法获得想要的行为.

I have tried all sorts of things but cannot get the behaviour I am after.

用户按下按钮,将新行添加到数据网格中,单元格将进入编辑模式.

User press a button new Row is added to datagrid and cell gets focus in edit mode.

我正在使用MVVM模式,当用户按下按钮时,我向observableCollection添加了一条记录.

I am using a MVVM pattern and At the moment when a user presses the button I add a record to the observableCollection.

但是我被困住了.我找到了以下内容并尝试实现,但出现一个错误,提示者为空.

but then I am stuck. I have found the following and tried to implement but i get an error that the presenter is null.

 static class DataGridHelper



 {



 static public int GetRowIndex(DataGrid dg, DataGridCellInfo dgci)



 {



  DataGridRow dgrow = (DataGridRow)dg.ItemContainerGenerator.ContainerFromItem(dgci.Item);



  return dgrow.GetIndex();



 }







 static public int GetColIndex(DataGrid dg, DataGridCellInfo dgci)



 {



  return dgci.Column.DisplayIndex;



 }







 static T GetVisualChild<T>(Visual parent) where T : Visual



 {



  T child = default(T);



  int numVisuals = VisualTreeHelper.GetChildrenCount(parent);



  for (int i = 0; i < numVisuals; i++)



  {



  Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);



  child = v as T;



  if (child == null)



  {



  child = GetVisualChild<T>(v);



  }



  if (child != null)



  {



  break;



  }



  }



  return child;



 }







 static public DataGridCell GetCell(DataGrid dg, int row, int column)



 {



  DataGridRow rowContainer = GetRow(dg, row);







  if (rowContainer != null)



  {



  DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);







  // try to get the cell but it may possibly be virtualized



  DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);



  if (cell == null)



  {



  // now try to bring into view and retreive the cell



  dg.ScrollIntoView(rowContainer, dg.Columns[column]);



  cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);



  }



  return cell;



  }



  return null;



 }











 static public DataGridRow GetRow(DataGrid dg, int index)



 {



  DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index);



  if (row == null)



  {



  // may be virtualized, bring into view and try again



  dg.ScrollIntoView(dg.Items[index]);



  row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index);



  }



  return row;



 }



 }



推荐答案

您可以尝试在getRow方法中的dg.ScrollIntoView(dg.Items [index])之前调用dg.UpdateLayout().

You can try to call dg.UpdateLayout() before dg.ScrollIntoView(dg.Items[index]) in getRow method.

托尼


这篇关于添加新行并编辑第一个单元格数据网格wpf(我要放弃)紧急的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆