Based on some standard web searches, I narrowed down my problem: I believe that the event that triggers my storyboard is fired before the template expands. Thus, the names do not make sense, and the links to the names made by the storyboard animation are null.
This will not be a problem if I do not work with the ControlTemplate. I could just snap to the event after updating the layout, and then manually call it the first time. The problem is solved. However, since this is a ControlTemplate control in its own XAML dictionary file, I cannot use C # to solve this problem.
(Update: I can definitively say that this is not an order problem - in other words, it has nothing to do with defining content before ControlTemplate.Resources or similar. However, similar problems can be caused by such ordering issues, so this question is worth exploring if you encounter similar problems. See one of the answers below before this update for a more detailed explanation.)
And again, I could be on the wrong track entirely. This is just my understanding of what is happening behind the curtain. So that you can judge for yourself, here is the actual exception:
System.InvalidOperationException:
{"'PART_UnderlineBrush' name cannot be found in the namespace 'System.Windows.Controls.ControlTemplate'." }
Here is a style / template for reference, while all unnecessary things (storyboards, properties, etc.) are deleted.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Style x:Key="MetroTabItem" TargetType="{x:Type TabItem}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type TabItem}"> <Border> <Border.BorderBrush> <SolidColorBrush Color="#00ffffff" x:Name="PART_UnderlineBrush" /> </Border.BorderBrush> <ContentPresenter Content="{TemplateBinding Header}" ContentTemplate="{TemplateBinding HeaderTemplate}" /> </Border> <ControlTemplate.Resources> <Storyboard x:Key="SelectTab"> <ColorAnimation BeginTime="0:0:0" Duration="0:0:0.5" Storyboard.TargetProperty="Color" Storyboard.TargetName="PART_UnderlineBrush" To="#ddffffff" /> </Storyboard> </ControlTemplate.Resources> <ControlTemplate.Triggers> <EventTrigger RoutedEvent="Selector.Selected"> <BeginStoryboard Name="BeginSelected" Storyboard="{StaticResource SelectTab}" /> </EventTrigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </ResourceDictionary>
source share