C# 向datagrid添加行并从每个单元格中检索新值
我需要得到一行中每个单元格的每个值。首先,当我将数据添加到新行时。数据在按enter键后消失-但是它在我的C# 向datagrid添加行并从每个单元格中检索新值,c#,wpf,datagrid,C#,Wpf,Datagrid,我需要得到一行中每个单元格的每个值。首先,当我将数据添加到新行时。数据在按enter键后消失-但是它在我的可观察到的集合中 我有一个observateCollection,我将其设置为可以重用Datagrid。例如,我有一个ObservableCollection,其中包含多个CommissionOperator(db/linq对象)对象。在我将新数据插入到网格中之后,我在我的observedcollection中得到10个CommissionOperator对象,在我的observedcoll
可观察到的集合中
我有一个observateCollection
,我将其设置为可以重用Datagrid
。例如,我有一个ObservableCollection
,其中包含多个CommissionOperator
(db/linq对象)对象。在我将新数据插入到网格中之后,我在我的observedcollection
中得到10个CommissionOperator
对象,在我的observedcollection
中得到一个对象
现在,我试图找出如何从Datagrid
获取值。所以我做了一些研究,得出了这个结论(从一个我目前找不到链接的问题):
现在我得到一个错误,说我不能将TextBlock强制转换成TexBox
,而TextBlock.Text
是空的
当我将数据输入到新行时,我只是想弄清楚如何从DataGrid
获取数据
以下是添加新行后我的ObservableCollection
的外观:
以下是添加期间DataGrid
的方式(仅供参考)
这是插入后的DataGrid
我想如果我可以从单元格中获取数据,我可以重新绑定DataGrid
,或者更新绑定或其他内容,以使其显示正确的数据
这可能是因为数据正在从我的DataGrid
中消失,我实际上无法从该单元格获取数据吗?您不需要这些。您不能在WPF中“从UI读取数据”,因为。创建一个合适的视图模型,或者从数据项中读取数据。我有一个合适的视图模型,它是我的SelectionData
observateCollection
所在的位置。。。但是,我无法从中获取值,比如说,SelectionData[10]
从ObservableCollection
的pic获取值,因为它没有属性-因此我无法使用反射获取值……请将您的集合更改为强类型集合,而不是对象的集合。否则,请让ViewModel创建新项目,而不是视图。视图不负责管理数据项,只负责显示数据项。这就是问题所在-我希望能够用尽可能少的代码重用网格…通过创建强类型集合,我将无法选择这样做。让ViewModel创建新项目是什么意思(在没有用户添加新数据的情况下,我如何才能做到这一点)?
//in a class that inherits from DataGrid
public Microsoft.Windows.Controls.DataGridCell GetCell( int column )
{
Microsoft.Windows.Controls.DataGridRow rowContainer = GetRow(this.SelectedIndex);
if (rowContainer != null)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>( rowContainer );
// Try to get the cell but it may possibly be virtualized.
Microsoft.Windows.Controls.DataGridCell cell = (Microsoft.Windows.Controls.DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex( column );
if (cell == null)
{
// Now try to bring into view and retreive the cell.
this.ScrollIntoView( rowContainer, this.Columns[column] );
cell = (Microsoft.Windows.Controls.DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex( column );
}
return cell;
}
return null;
}
public Microsoft.Windows.Controls.DataGridRow GetRow( int _currentRowIndex )
{
Microsoft.Windows.Controls.DataGridRow row = (Microsoft.Windows.Controls.DataGridRow)this.ItemContainerGenerator.ContainerFromIndex( _currentRowIndex );
if (row == null)
{
// May be virtualized, bring into view and try again.
this.UpdateLayout();
this.ScrollIntoView( this.Items[_currentRowIndex] );
row = (Microsoft.Windows.Controls.DataGridRow)this.ItemContainerGenerator.ContainerFromIndex( _currentRowIndex );
}
return row;
}
public 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;
}
((TextBox) (RGVDataSelector.GetCell(1).Content)).Text