开发者

Getting WPF Templated items from Control and Data templates

开发者 https://www.devze.com 2022-12-20 03:01 出处:网络
How do I get a named control from a contr开发者_如何学Gool that is bound to a control template or data template?

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

精彩评论

暂无评论...
验证码 换一张
取 消