簡體   English   中英

由於綁定到在模板布局之前觸發的事件,因此命名范圍問題

[英]Name scope issues due to binding to event that gets fired before template layout

基於一些標准的網絡搜索,我已經將我的問題縮小到這個:我相信在模板擴展之前調用觸發我的故事板的事件。 因此,名稱毫無意義,故事板動畫制作的名稱引用為空。

如果我不使用ControlTemplate,這不會是一個問題。 我可以在更新布局后綁定到事件,然后在第一次手動調用它。 問題解決了。 但是,由於這是自己的資源字典XAML文件中的ControlTemplate,我無法使用C#來解決此問題。

(更新:我可以肯定地說這不是一個排序問題 - 換句話說,它與在ControlTemplate.Resources或類似之前定義內容無關。但是,類似的問題可能是由這樣的排序問題引起的,所以如果您遇到類似的問題,這個問題值得研究。請參閱此更新前的以下答案之一,以獲得更詳細的解釋。)

然后,我可能完全走錯了軌道。 這只是我對窗簾背后發生的事情的理解。 所以你可以自己判斷,這是實際的例外:

System.InvalidOperationException:
{''PART_UnderlineBrush'名稱在'System.Windows.Controls.ControlTemplate'的名稱范圍內找不到。“}

這是參考的樣式/模板,刪除了所有額外的東西(故事板,屬性等)。

<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>

我不知道這是否會完全符合您的要求,但如果您使用TabItem的IsSelected屬性的Trigger替換EventTrigger ,它可能會起作用:

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

原始代碼似乎確實存在一些時序問題。 Selector.Selected事件似乎在控件加載之前被觸發。

把你的

<ControlTemplate.Resources>

之前

<Border>

完整代碼:

<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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM