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

发布于 2024-11-19 00:26:15 字数 4098 浏览 2 评论 0原文

我在使用 EventTriggers 时遇到问题。我有两个按钮和两个 EventTriggers,我想监听这些按钮的 Click 事件,然后触发器应该启动一个 StoryBoard,该 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) 在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,对象 obj,布尔开始) --- 内部异常堆栈跟踪结束 ---

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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溺孤伤于心 2024-11-26 00:26:15

由于您的按钮“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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文