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:
Here is the whole code:
///
public static IEnumerator
{
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
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 :)
Filed under: Silverlight