views:

106

answers:

1

Hi

we are trying to add ColumnDefinition to a Grid in WPF dynamically from code behind. The question is: when do you do that? In the Usercontrol's Loaded event? We have a ListView that utilizes a DataTemplate which in turn uses the Grid. How do you access that Grid instance (the one inside a DataTemplate) from code behind? It does have a x:Name but it is not visible in the UserControl's code. Thanks for your help.

+1  A: 

You can walk the logical tree using the VisualTreeHelper, then programatically create column definitions.

Here is a good article on using the Visual and Logical trees and how to navigate them effectively.

Kilhoffer
Exactly. I usually find a visual element (contained in the Grid) by name and then use the VisualTreeHelper to obtain that element's 'Parent', which is the logical element you're looking for.