To find an element within the template after the template has been applied, you can call the FindName method of the Template. I update your code as follows. For more information, you can also refer here.
The code of MainWindow.xaml.cs:
private void Button_Click(object sender, RoutedEventArgs e)
{
Button btnContent = (Button)tabControl.Template.FindName("testBtn", tabControl);
MessageBox.Show(btnContent.GetValue(Button.ContentProperty).ToString());
}
Button in ControlTemplate:
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Grid.Column="0">
<Button Name="testBtn" Content="My Button..."/>
</StackPanel>
Give TabControl a name and add a button:
<StackPanel>
<TabControl Name="tabControl" Style="{DynamicResource TabControlStyle1}">
<TabItem Header="1"/>
<TabItem Header="2"/>
</TabControl>
<Button Content="click" Click="Button_Click"></Button>
</StackPanel>
The pricture of result:
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.