如何在Silverlight中查找可见的DataGrid行?
答案 0 :(得分:1)
我不确定Visible DataGridRow
的含义,但是您可以通过在Visual Tree中找到它们来获取当前生成的所有DataGridRow
。由于DataGridRow
DataGrid
以及可能的更多内容
示例强>
private List<DataGridRow> GetDataGridRows(DataGrid dataGrid)
{
return GetVisualChildCollection<DataGridRow>(c_dataGrid);
}
<强> GetVisualChildCollection 强>
public static List<T> GetVisualChildCollection<T>(object parent) where T : FrameworkElement
{
List<T> visualCollection = new List<T>();
GetVisualChildCollection(parent as DependencyObject, visualCollection);
return visualCollection;
}
private static void GetVisualChildCollection<T>(DependencyObject parent, List<T> visualCollection) where T : FrameworkElement
{
int count = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < count; i++)
{
DependencyObject child = VisualTreeHelper.GetChild(parent, i);
if (child is T)
{
visualCollection.Add(child as T);
}
else if (child != null)
{
GetVisualChildCollection(child, visualCollection);
}
}
}
答案 1 :(得分:0)
我这样做的方法是联系DataGrid的LoadingRow和UnloadingRow事件。
这是一个例子
HashSet<DataGridRow> loadedRows
private void HandleUnloadingRow(object sender, DataGridRowEventArgs e)
{
_loadedRows.Remove(e.Row);
}
private void HandleLoadingRow(object sender, DataGridRowEventArgs e)
{
_loadedRows.Add(e.Row);
}