针对特定用户界面元素的EventTrigger问题

时间:2011-07-07 08:34:06

标签: wpf storyboard eventtrigger

使用EventTriggers时遇到问题。我有两个按钮和两个EventTriggers,我想听这些按钮中的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'。 ---&GT; System.ArgumentException:找不到名称为“TriggerElement1”的FrameworkElement。      在System.Windows.FrameworkElement.FindNamedFrameworkElement(FrameworkElement startElement,String targetName)      在System.Windows.EventTrigger.ProcessOneTrigger(FrameworkElement triggersHost,TriggerBase triggerBase)      在System.Windows.EventTrigger.ProcessTriggerCollection(FrameworkElement triggersHost)      在System.Windows.FrameworkElement.TryFireInitialized()      在System.Windows.FrameworkElement.EndInit()      在MS.Internal.Xaml.Runtime.ClrObjectRuntime.InitializationGuard(XamlType xamlType,Object obj,Boolean begin)      ---内部异常堆栈跟踪结束---

删除EventTrigger SourceName属性可修复错误,但我需要根据单击的按钮启动不同的StoryBoards。所以据我所知,我需要使用SourceName。

为什么WPF找不到名为TriggerElement1的按钮?我认为RoutedEvents会在可视化树上冒泡,所以只要触发器处于足够高的水平,它应该没有命名问题?找到名为Panel1的StoryBoard.Target没有问题。我是否采取了正确的方法来获得两个按钮来触发不同的故事板?

1 个答案:

答案 0 :(得分:0)

由于您的按钮“TriggerElement1”位于ContentPresenter应用程序中,因此无法在初始化时找到该按钮。因为contentpresentor内容将在运行时呈现。

我对您的代码进行了一些更改,现在工作正常...希望这可以解决您的问题。

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