I am having an issue when using EventTriggers. I have two buttons and two EventTriggers that I want to listen for Click events from those buttons, then the triggers should start a StoryBoard which animates the height of a control.
I have no problem with starting the StoryBoards, I simply have a problem with specifying the SourceName of the EventTrigger:<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>
With this I get a run time error:
The most likely causing exception was was: 'System.Windows.Markup.XamlParseException: 'Initialization of 'System.Windows.Controls.Grid' threw an exception.' Line number '23' and line position '6'. ---> System.ArgumentException: Cannot find a FrameworkElement with Name 'TriggerElement1'. at 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, Object obj, Boolean begin) --- End of inner exception stack trace ---
Removing the EventTrigger SourceName property fixes the error, but I need to start different StoryBoards depending on which button is clicked. So as far as I know I need to use the SourceNam开发者_开发百科e.
Why can't WPF find my Button called TriggerElement1
? I thought RoutedEvents get bubbled up the visual tree, so as long as the trigger is at a high enough level it should have no naming problems? It has no problem finding the StoryBoard.Target called Panel1. Have I taken the right approach to get two buttons to trigger different storyboards?
Since your button "TriggerElement1" is inside a ContentPresenter App is unable to locate the button at initalization. Because contentpresentor content will be rendered at runtime.
I made few changes to your code which is working fine now... hope this will solve your problem.
<UserControl x:Class="WpfApplication1.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" />
<DoubleAnimation To="0" Duration="0:0:0.75" Storyboard.TargetName="Panel2" Storyboard.TargetProperty="Height" />
</Storyboard>
<Storyboard x:Key="GrowPanel2">
<DoubleAnimation To="400" Duration="0:0:0.75" Storyboard.TargetName="Panel2" Storyboard.TargetProperty="Height" />
<DoubleAnimation To="0" Duration="0:0:0.75" Storyboard.TargetName="Panel1" Storyboard.TargetProperty="Height" />
</Storyboard>
</UserControl.Resources>
<Grid>
<Border BorderBrush="HotPink" BorderThickness="2">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<StackPanel Orientation="Horizontal" Grid.Column="1" >
<Button x:Name="TriggerElement2" Background="BurlyWood" Width="30" Height="30" VerticalAlignment="Top">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<EventTrigger.Actions>
<BeginStoryboard Storyboard="{StaticResource GrowPanel2}" />
</EventTrigger.Actions>
</EventTrigger>
</Button.Triggers>
</Button>
<Button x:Name="TriggerElement1" Background="Chartreuse" Width="30" Height="30" VerticalAlignment="Top" >
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<EventTrigger.Actions>
<BeginStoryboard Storyboard="{StaticResource GrowPanel1}" />
</EventTrigger.Actions>
</EventTrigger>
</Button.Triggers>
</Button>
</StackPanel>
<Grid Grid.Column="0" Grid.ColumnSpan="2" VerticalAlignment="Bottom">
<Grid Name="Panel1" Height="0" VerticalAlignment="Top" Background="Red"/>
<Grid x:Name="Panel2" Height="0" VerticalAlignment="Top" Background="Blue"/>
</Grid>
</Grid>
</Border>
</Grid>
</UserControl>
精彩评论