Is GetTemplateChild Obsolete in .Net 3.5 and what is the difference between FrameWorkTemplate.FindName and ControlTemplate.FindName
c#, controltemplate, wpf
Solution
1.
The only difference is that `GetTemplateChild` will return `null` when:
- no template exists, or
- the named object exists but is not a `DependencyObject` (I don't know if this is even possible).
Otherwise, this code:
this.GetTemplateChild(name)
is literally equivalent to this code:
this.Template.FindName(name, this)
I don't know why it was marked "Do not use". I notice that this note existed on the MSDN page for version 3 and was removed from version 3.5, but I don't know if that means anything.
2.
Did you mean `FrameworkElement`? Like Jens said, `ControlTemplate` inherits `FrameworkTemplate`, and they both use the `FrameworkTemplate.FindName` method. But FrameworkTemplate.FindName and FrameworkElement.FindName are different. `FrameworkElement.FindName` only looks inside child elements but not the template, while `FrameworkTemplate.FindName` only looks at the template's own elements but not the parent element it's applied to.
Problem
I overrided Template of a control in ResourceDictionary `Generic.xaml`. In that 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 Loaded event of control I did ``` Button b = (Button)mycontrol.Template.FindName("PARTName", mycontrol) //Add Events on my button ``` Some where on Internet I read that I can do ``` public override void OnApplyTemplate() { base.OnApplyTemplate(); UIElement editingelement = GetTemplateChild("PART_EditingElement"); if (editingelement != null) { // do something } } ``` When I tried doing this suggestion for `GetTemplateChild` says Do Not Use. So my question is Why not to use `GetTemplateChild`. Is it obsolete? And What is the difference between `FrameWorkTemplate.FindName` and `ControlTemplate.FindName`?