June 12, 2014 08:36 by
Peter
A data grid view is a rectangular control made of columns and rows. I have a DataGrid where I have included some controls in column header. Each column is a Template column. These controls appear just below the column header which are used for entering filter information. Here's the issue on my code on Silverlight 5.
VisualTreeHelper class helps to iterate through the visual tree of the xaml. Using it we can find the child and parent controls of the rendered controls. Lets check the Visual Tree of the rendered control using Silverlight Spy.
The Following Method do a search over the child controls with in a control recursively and returns the control based on Name.
private object GetChildControl(DependencyObject parent, string controlName)
{
Object tempObj = null;
int count = VisualTreeHelper.GetChildrenCount(parent);
for (int counter = 0; counter < count; counter++)
{
//Get The Child Control based on Index
tempObj = VisualTreeHelper.GetChild(parent, counter);
//If Control's name Property matches with the argument control
//name supplied then Return Control
if ((tempObj as DependencyObject).GetValue(NameProperty).ToString() == controlName)
return tempObj;
else //Else Search Recursively
{
tempObj = GetChildControl(tempObj as DependencyObject, controlName);
if (tempObj != null)
return tempObj;
}
}
return null;
}
Make sure that the same has to be delegated to UI thread using Dispatcher. As the controls created using UI Thread can not be accessed from other thread.
//Access the Grid Header Controls
Dispatcher.BeginInvoke(delegate
{
var hyperlinkControl = GetChildControl(dataGrid1, "hlSort");
var checkControl = GetChildControl(dataGrid1, "chkSelectAll");
});