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:
/// <summary>
/// Extends the DataGrid.
/// </summary>
public static class DataGridExtensions
{
/// <summary>
/// Gets the list of DataGridRow objects.
/// </summary>
/// <param name="grid">The grid wirhrows.</param>
/// <returns>List of rows of the grid.</returns>
public static ICollection<DataGridRow> GetRows(this DataGrid grid)
{
List<DataGridRow> rows = new List<DataGridRow>();
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<DataGridRow> GetRowsEnumerator(this DataGrid grid)
{
return new GridRowEnumerator(grid);
}
And here is the implementation of enumerator:
public class GridRowEnumerator : IEnumerator<DataGridRow>
{
private DataGrid m_Grid;
private IEnumerator m_Enumerator;
public GridRowEnumerator(DataGrid grid)
{
m_Grid = grid;
m_Enumerator = m_Grid.ItemsSource.GetEnumerator();
}
#region IEnumerator<DataGridRow> 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
分享到:
相关推荐
Professional Silverlight 4 prepares Web developers to take full advantage of the newest release of the most popular platform for developing rich interactive applications. Coverage Includes: * ...
Everything .NET developers need to take advantage of Silverlight 4 Silverlight 4 is a major new release of Microsoft's flagship product for building rich, interactive applications that combine ...
WebAssembly: Accessing C and C++ in Web Applications English | MP4 | AVC 1280×720 | AAC 48KHz 2ch | 1h 45m | 213 MB Accelerate web applications with native code using WebAssembly. WebAssembly is a ...
web dynpro demo\Accessing ABAP Functions in Web Dynpro Java.pdf
Accessing Variables in Block Objects 初学者学习代码
learn-by-doing tutorial, you'll learn how to create interactive user interfaces for the Web and the newest version of Windows Phone by mastering the essential tools and techniques for Silverlight. ...
Accessing-Databases-JDBC:如何通过jdbc访问数据库,英文参考资料
What you'll learn WF 4.0 basics New activities and changes to existing activities in WF 4.0 Customizing your workflows Accessing your workflows in a variety of ways in a variety of situations Using ...
A qualified operation is one that affects only specific rows that satisfy the conditions of a WHERE clause, as opposed to accessing the whole table. An index can have multiple node levels An index ...
Researchers has published lots of stuff on different-2 exploitation techniques for conducting various type of attacks including accessing data stored in database, reading/writing code from/to server ...
accessing services in a dynamically changing networked environment. A service-oriented network socket takes a high-level description of a service and opportunistically connects to the best provider of...
What youll learn * WF 4 basics * New activities and changes to existing activities in WF 4 * Customizing your workflows * Accessing your workflows in a variety of ways in a variety of situations * ...
New : Autosizing columns / rows on double-click in TTMSFMXGrid New : Column persistence in TTMSFMXGrid Improved : Data reset when toggling active in TTMSFMXScope Fixed : Issue with checkbox and ...
ASP.NET 3.5 brings the power of Visual Studio® 2008 along with the multitude of language improvements in C# 2008 and Visual Basic® 2008 as well as powerful new technology called LINQ, together with ...
This chapter starts with an example of introspecting an Excel workbook (i.e., presenting how many worksheets the workbook contains, the names of the worksheets, and the number of rows and columns in ...
VB.NET - Client-side example accessing data stored in session. 服务端的例子: C#.NET - Server-side example accessing data stored in session. C#.NET - Server-side example which includes: MasterPage...
VB.NET - Client-side example accessing data stored in session. 服务端的例子: C#.NET - Server-side example accessing data stored in session. C#.NET - Server-side example which includes: MasterPage...
CCNA Exploration 4 Accessing the WAN章节练习题
When it comes to programmatically accessing Microsoft's Active Directory a lot of people seem to have quite a difficult time tying all the pieces together to accomplish exactly what they want to....