Since a template can be created several times, it is not possible to link the generated element via x:Name . Instead, you need to find the named element in the template applied to the control.
For simplified XAML:
<ControlTemplate x:Key="MyTemplate"> <Ellipse x:Name="MyEllipse" /> </ControlTemplate>
You would do something like this:
var template = (ControlTemplate)FindResource("MyTemplate"); template.FindName("MyEllipse", myControl);
Or even simpler:
var ellipse = (Ellipse)myControl.Template.FindName("MyEllipse", myControl);
You can read about FrameworkTemplate.FindName .
Some examples and discussion here , here and here .
Drew noakes
source share