TheWommies
TheWommies

Reputation: 5072

Getting WPF Templated items from Control and Data templates

How do I get a named control from a control 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.

Upvotes: 1

Views: 289

Answers (1)

Aran Mulholland
Aran Mulholland

Reputation: 23935

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;
 }

Upvotes: 1

Related Questions