If you have ever worked with a DataGrid and columnHeader template then you might have faced a scenario where you need to access the controls defined in a style. In this post we will discuss how to retrieve the same at runtime from the code behind.The IssueAs shown above, the first column of the grid is assigned with a ColumnHeader style which holds 2 controls.Now the highlighted controls need to be retrieved from the code behind.The SolutionThe 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 , if you missed my post on this must have tool then feel free to have a look here.The Following Method do a search over the child controls with in a control recursively and returns the control based on Name?1234567891011121314151617181920212223private 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;}While implementing the method 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.?123456//Access the Grid Header ControlsDispatcher.BeginInvoke(delegate{ var hyperlinkControl = GetChildControl(dataGrid1, "hlSort"); var checkControl = GetChildControl(dataGrid1, "chkSelectAll");});Hope this will help .
If you have ever worked with a DataGrid and columnHeader template then you might have faced a scenario where you need to access the controls defined in a style. In this post we will discuss how to retrieve the same at runtime from the code behind.
As shown above, the first column of the grid is assigned with a ColumnHeader style which holds 2 controls.
Now the highlighted controls need to be retrieved from the code behind.
The 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 , if you missed my post on this must have tool then feel free to have a look here.
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
(
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
DependencyObject, controlName);
(tempObj !=
)
}
While implementing the method 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"
});
Hope this will help .
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: