How do I get a named control from a contr开发者_如何学Gool that is bound to a control template or data template?
I have tried FindName it does not work. I prefer not to use the VisualTreeHelper as you have to traverse through each parent child item individually.
It depends when you do it. If you do it in the constructor it wont work as the element only exists after the template has been applied.
This is the standard way to do it if you create the control:
public override void OnApplyTemplate() {
//i call the base first
base.OnApplyTemplate();
//then go looking for the newly created elements
TextBox textBox = this.Template.FindName("PART_TextBox", this) as TextBox;
}
精彩评论