Name name issues due to event binding that fires before layout template

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> <!-- This is the element that I need to reference, but I am unable to do so. --> <SolidColorBrush Color="#00ffffff" x:Name="PART_UnderlineBrush" /> </Border.BorderBrush> <ContentPresenter Content="{TemplateBinding Header}" ContentTemplate="{TemplateBinding HeaderTemplate}" /> </Border> <ControlTemplate.Resources> <Storyboard x:Key="SelectTab"> <!-- This is the animation that will always fail, due to the name reference. --> <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> 
+4
source share
2 answers

I don't know if this will do exactly what you want, but if you replace EventTrigger with Trigger for the IsSelected property in TabItem, this might work:

 <ControlTemplate.Triggers> <Trigger Property="IsSelected" Value="True"> <Trigger.EnterActions> <BeginStoryboard Name="BeginSelected" Storyboard="{StaticResource SelectTab}" /> </Trigger.EnterActions> </Trigger> </ControlTemplate.Triggers> 

There seems to be some problem with the source code. The Selector.Selected event seems to fire before the control loads.

+2
source

Put your

 <ControlTemplate.Resources> 

front

 <Border> 

Full code:

 <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}"> <ControlTemplate.Resources> <Storyboard x:Key="SelectTab"> <!-- This is the animation that will always fail, due to the name reference. --> <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> <Border> <Border.BorderBrush> <!-- This is the element that I need to reference, but I am unable to do so. --> <SolidColorBrush Color="#00ffffff" x:Name="PART_UnderlineBrush" /> </Border.BorderBrush> <ContentPresenter Content="{TemplateBinding Header}" ContentTemplate="{TemplateBinding HeaderTemplate}" /> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> 

+1
source

Source: https://habr.com/ru/post/1394685/


All Articles