4

いくつかの標準的な Web 検索に基づいて、問題をこれに絞り込みました。テンプレートが展開される前に、ストーリーボードを起動するイベントが呼び出されると思います。したがって、名前は無意味であり、ストーリーボードのアニメーションによって作成された名前の参照は null です。

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

2 に答える 2

2

これがあなたの望むとおりに機能するかどうかはわかりませんが、TabItem の IsSelected プロパティのEventTriggera を a に置き換えると、うまくいく可能性があります。Trigger

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

元のコードで発生しているタイミングの問題があるようです。Selector.Selected イベントは、コントロールが読み込まれる前に発生したようです。

于 2012-02-04T08:34:57.383 に答える
1

あなたの

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

于 2012-02-03T20:52:14.003 に答える