EventTriggersを使用するときに問題が発生します。2つのボタンと2つのEventTriggerがあり、それらのボタンからClickイベントをリッスンしたい場合、トリガーはコントロールの高さをアニメーション化するStoryBoardを開始する必要があります。
StoryBoardsの起動に問題はありません。単に、EventTriggerのSourceNameの指定に問題があります。
<UserControl x:Class="MyWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
>
<UserControl.Resources>
<Storyboard x:Key="GrowPanel1">
<DoubleAnimation To="400" Duration="0:0:0.75" Storyboard.TargetName="Panel1" Storyboard.TargetProperty="Height" />
</Storyboard>
<Storyboard x:Key="GrowPanel2">
<DoubleAnimation To="400" Duration="0:0:0.75" Storyboard.TargetName="Panel2" Storyboard.TargetProperty="Height" />
</Storyboard>
</UserControl.Resources>
<Grid >
<Grid.Triggers>
<EventTrigger RoutedEvent="Button.Click" SourceName="TriggerElement1" >
<EventTrigger.Actions>
<BeginStoryboard Storyboard="{StaticResource GrowPanel1}" />
</EventTrigger.Actions>
</EventTrigger>
<EventTrigger RoutedEvent="Button.Click" SourceName="TriggerElement2" >
<EventTrigger.Actions>
<BeginStoryboard Storyboard="{StaticResource GrowPanel2}" />
</EventTrigger.Actions>
</EventTrigger>
</Grid.Triggers>
<Border BorderBrush="HotPink" BorderThickness="2">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ContentPresenter Panel.ZIndex="20" Grid.Column="1" >
<ContentPresenter.Content>
<StackPanel Orientation="Horizontal">
<Button x:Name="TriggerElement2" Background="BurlyWood" Width="30" Height="30" VerticalAlignment="Top" />
<Button x:Name="TriggerElement1" Background="Chartreuse" Width="30" Height="30" VerticalAlignment="Top" />
</StackPanel>
</ContentPresenter.Content>
</ContentPresenter>
<Grid Grid.Column="0" Grid.ColumnSpan="2">
<my1:TreeViewNav Name="Panel1" Height="0" VerticalAlignment="Top" />
<my:ListViewNav x:Name="Panel2" Height="0" VerticalAlignment="Top" />
</Grid>
</Grid>
</Border>
</Grid>
</UserControl>
これにより、実行時エラーが発生します。
最も可能性の高い例外の原因は次のとおりです。'System.Windows.Markup.XamlParseException:'System.Windows.Controls.Gridの初期化で例外がスローされました。行番号「23」および行位置「6」。---> System.ArgumentException:「TriggerElement1」という名前のFrameworkElementが見つかりません。System.Windows.FrameworkElement.FindNamedFrameworkElement(FrameworkElement startElement、String targetName)at System.Windows.EventTrigger.ProcessOneTrigger(FrameworkElement triggersHost、TriggerBase triggerBase)at System.Windows.EventTrigger.ProcessTriggerCollection(FrameworkElement triggersHost)at System.Windows.FrameworkElement.TryFireInitialized ()at System.Windows.FrameworkElement.EndInit()at MS.Internal.Xaml.Runtime.ClrObjectRuntime.InitializationGuard(XamlType xamlType、
EventTrigger SourceNameプロパティを削除するとエラーが修正されますが、クリックされたボタンに応じて異なるStoryBoardを開始する必要があります。私の知る限り、SourceNameを使用する必要があります。
なぜWPFは私のボタンが呼び出されないのTriggerElement1
ですか?RoutedEventsがビジュアルツリーにバブルアップすると思いましたが、トリガーが十分に高いレベルにある限り、名前の問題は発生しませんか?Panel1というStoryBoard.Targetを見つけるのに問題はありません。異なるストーリーボードをトリガーする2つのボタンを取得するための正しいアプローチを取りましたか?