How and when to dynamically add columns to a Grid in WPF
we are trying to add Co开发者_如何学GolumnDefinition 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.
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.
精彩评论