How does WPF access controls in inline templates?

Hanying Li 1 Reputation point
2021-05-05T08:54:22.527+00:00

for example:
<ToggleButton x:Name="FlipButton">
<ToggleButton.Template>
<ControlTemplate>
<Grid Margin="0,5,0,0">
<Path Data="M0,0 L8,8 0,16" Stroke="White">
<Path.RenderTransform>
<RotateTransform x:Name="Tag"/>
</Path.RenderTransform>
</Path>
</Grid>
</ControlTemplate>
</ToggleButton.Template>
</ToggleButton>
I want to animate the "Tag" object in the inline template at some point, but when I apply the animation, I will be prompted that the name cannot be found. Is there any way to deal with this problem?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,678 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. DaisyTian-1203 11,616 Reputation points
    2021-05-05T09:50:51.123+00:00

    You can use below code to get RotateTransform named Tag .

     RotateTransform tag = (RotateTransform)FlipButton.Template.FindName("Tag", FlipButton);  
    

    There is a Microsoft document How to: Find ControlTemplate-Generated Elements giving a detailed description of it. By the way, if I misunderstand your question, please point out.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments