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?
1.
The only difference is that GetTemplateChild will return null when:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With