Is GetTemplateChild deprecated in .Net 3.5 and what is the difference between FrameWorkTemplate.FindName and ControlTemplate.FindName

I redefined the control template in ResourceDictionary Generic.xaml . In this I added a button on which I wanted to add some events.

 <Setter Property="Template"> <Setter.Value> --Added my button here. </Setter.Value> </Setter> 

So in the Loaded event of control I did

 Button b = (Button)mycontrol.Template.FindName("PARTName", mycontrol) //Add Events on my button 

Some where on the Internet I read what I can do

 public override void OnApplyTemplate() { base.OnApplyTemplate(); UIElement editingelement = GetTemplateChild("PART_EditingElement"); if (editingelement != null) { // do something } } 

When I tried to make this offer for GetTemplateChild , do not use.

enter image description here

So my question is:

  • Why not use GetTemplateChild . Is it out of date? AND

  • What is the difference between FrameWorkTemplate.FindName and ControlTemplate.FindName ?

+4
source share
1 answer

1.

The only difference is that GetTemplateChild will return null when:

  • No template, or
  • A named object exists, but is not a DependencyObject (I don't know if this is possible at all).

Otherwise, this code:

 this.GetTemplateChild(name) 

literally equivalent to this code:

 this.Template.FindName(name, this) 

I do not know why it was marked "Do not use." I noticed that this note existed on the MSDN page for version 3 and was removed from version 3.5 , but I don't know what that means.


2.

Did you mean FrameworkElement ? As Jens said, a ControlTemplate inherits a FrameworkTemplate , and both of them use the FrameworkTemplate.FindName method. But FrameworkTemplate.FindName and FrameworkElement.FindName are different. FrameworkElement.FindName only looks at the child elements, but not the template, but FrameworkTemplate.FindName looks only at the template's own elements, but does not apply to its parent element.

+4
source

Source: https://habr.com/ru/post/1478917/


All Articles