views:

58

answers:

1

Hi,

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 chld item individually.

Thanks

+1  A: 

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