最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

AccessingofRowsinSilverlightDataGrid

来源:动视网 责编:小采 时间:2020-11-09 07:36:45
文档

AccessingofRowsinSilverlightDataGrid

AccessingofRowsinSilverlightDataGrid:Imagine you want to enumerate (enlist) all rows (DataGridRow) of Silverlight Grid (DataGrid). By design this is not very simple tasks. For example, you want to do something like this: foreach (DataGridRow rowItem in grid.Rows) { . . . } Th
推荐度:
导读AccessingofRowsinSilverlightDataGrid:Imagine you want to enumerate (enlist) all rows (DataGridRow) of Silverlight Grid (DataGrid). By design this is not very simple tasks. For example, you want to do something like this: foreach (DataGridRow rowItem in grid.Rows) { . . . } Th


Imagine you want to enumerate (enlist) all rows (DataGridRow) of Silverlight Grid (DataGrid). By design this is not very simple tasks. For example, you want to do something like this: foreach (DataGridRow rowItem in grid.Rows) { . . . } Th

Imagine you want to enumerate (enlist) all rows (DataGridRow) of Silverlight Grid (DataGrid). By design this is not very simple tasks.
For example, you want to do something like this:

foreach (DataGridRow rowItem in grid.Rows)
{
. . .
}

This very important and very frequent requirement is just an issue. You will notice that this is almost impossible and will start to research in internet. Good luck. So, I decided to post the code of extension class which makes this possible:

foreach (DataGridRow rowItem in grid.GetRows())
{
. . .
}

Here is the whole code:

///


/// Extends the DataGrid.
///

public static class DataGridExtensions
{
///
/// Gets the list of DataGridRow objects.
///

/// The grid wirhrows.
/// List of rows of the grid.
public static ICollection GetRows(this DataGrid grid)
{
List rows = new List();

foreach (var rowItem in grid.ItemsSource)
{
// Ensures that all rows are loaded.
grid.ScrollIntoView(rowItem, grid.Columns.Last());

// Get the content of the cell.
FrameworkElement el = grid.Columns.Last().GetCellContent(rowItem);

// Retrieve the row which is parent of given element.
DataGridRow row = DataGridRow.GetRowContainingElement(el.Parent as FrameworkElement);

// Sometimes some rows for some reason can be null.
if (row != null)
rows.Add(row);
}

return rows;
}
}

The code above shows theoretically the idea of accessing of rows. Unfortunately this will work only if the whole grid result can be placed at the current view. While calling of ScrollIntoView() grid will reuse instances of created cells and rows and replace with new bounded data over and over again. The result of so called row virtualization will be replacing of rows in the list.
To workaround this, I implemented the right extension method

public static IEnumerator GetRowsEnumerator(this DataGrid grid)
{
return new GridRowEnumerator(grid);
}


And here is the implementation of enumerator:

public class GridRowEnumerator : IEnumerator
{
private DataGrid m_Grid;

private IEnumerator m_Enumerator;

public GridRowEnumerator(DataGrid grid)
{
m_Grid = grid;

m_Enumerator = m_Grid.ItemsSource.GetEnumerator();
}

#region IEnumerator Members

public DataGridRow Current
{
get
{
var rowItem = m_Enumerator.Current;

// Ensures that all rows are loaded.
m_Grid.ScrollIntoView(rowItem, m_Grid.Columns.Last());

// Get the content of the cell.
FrameworkElement el = m_Grid.Columns.Last().GetCellContent(rowItem);

// Retrieve the row which is parent of given element.
//DataGridRow row = DataGridRow.GetRowContainingElement(el);
DataGridRow row = DataGridRow.GetRowContainingElement(el.Parent as FrameworkElement);

return row;
}
}

#endregion

#region IDisposable Members

public void Dispose()
{

}

#endregion

#region IEnumerator Members

object IEnumerator.Current
{
get
{
return this.Current;
}
}

public bool MoveNext()
{
return m_Enumerator.MoveNext();
}

public void Reset()
{
m_Enumerator.Reset();
}

#endregion
}






This line I put here to measure how some interesting words can dramatically increase landing frequency of boring technical posts.

Bayern Inter Football Soccer champions league

Please forgive me for this :)


Posted May 02 2010, 12:30 AM by Damir Dobric

Filed under: Silverlight

文档

AccessingofRowsinSilverlightDataGrid

AccessingofRowsinSilverlightDataGrid:Imagine you want to enumerate (enlist) all rows (DataGridRow) of Silverlight Grid (DataGrid). By design this is not very simple tasks. For example, you want to do something like this: foreach (DataGridRow rowItem in grid.Rows) { . . . } Th
推荐度:
标签: of dat datagrid
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top