繁体   English   中英

仅使用XAML从WPF中的父窗口控制UserControl的Storyboard

[英]Controlling a UserControl's Storyboard from the parent Window in WPF using only XAML

我创建了一个非常简单的测试应用程序来尝试解决同事描述的这个问题。 他能够在C#中触发它,但我相信他需要更通用的解决方案,并希望它在XAML中严格完成。 问题是:如何触发UserControl内部的Storyboard开始响应包含Window中控件的事件(父级不必是Window,它可以是Page或另一个UserControl .. 。为了简单起见,我使用了一个窗口。 所以,我最好的例子是你有一个带有Button的Window和一个UserControl的实例。 您希望Button的Click事件触发UserControl内部的Storyboard开始。

在UserControl本身内部,Storyboard被声明为具有MyStoryboard键的资源。 我创建了一个带有EventTrigger的Button,以显示我可以通过使用Binding表达式引用它来触发Storyboard。 这是我的UserControl的完整XAML:

<UserControl x:Class="UserControlStoryboard.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300">
<UserControl.Resources>
    <Storyboard x:Key="MyStoryboard">
        <ColorAnimation Storyboard.TargetName="grid" Storyboard.TargetProperty="(Grid.Background).Color" From="CadetBlue" To="White" Duration="0:0:2" />
    </Storyboard>
</UserControl.Resources>
<Grid Background="CadetBlue" x:Name="grid">
    <Button Content="Press Me" Height="50">
        <Button.Triggers>
            <EventTrigger RoutedEvent="Button.Click">
                <BeginStoryboard>
                    <BeginStoryboard.Storyboard>
                        <Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type UserControl}}" Path="Resources[MyStoryboard]" />
                    </BeginStoryboard.Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Button.Triggers>
    </Button>            
</Grid>

我希望因为我能够通过引用一个Binding表达式告诉故事板,我应该能够从包含此UserControl作为子元素的Window中执行完全相同的操作。 但我似乎无法弄清楚如何在父窗口内获取对Storyboard的引用。 这只能在XAML中完成吗?

我的测试项目中的父窗口只有一个按钮和此UserControl的实例。 如果它甚至可能,哪个元素会让我添加一个EventTrigger,其中事件的源是Button,触发器操作告诉UserControl的Storyboard开始?

谢谢。

我认为在UserControl中响应外部事件的想法并不是一个非常安全的想法。 在UserControl中,您只想对控件中出生的事件做出反应。

你可以扭转局面并做以下事情:

  • 将需要动画的UC属性公开为依赖属性(在本例中为背景)
  • 在父窗口上创建EventTrigger和Storyboard

您可以使用StaticResource访问故事板。 您只需将资源分配给BeginStroyboard上的Storyboard。

<BeginStoryboard Storyboard="{StaticResource MyStoryboard}">
</BeginStoryboard>

我将代码更改为以下内容,当您按下按钮时,它将为您启动故事板。 希望这可以帮助。

    <UserControl.Resources>
    <Storyboard x:Key="MyStoryboard">
        <ColorAnimation Storyboard.TargetName="grid" Storyboard.TargetProperty="(Grid.Background).Color" From="CadetBlue" To="White" Duration="0:0:2" />
    </Storyboard>
</UserControl.Resources>
<Grid Background="CadetBlue" x:Name="grid">
    <Button Content="Press Me" Height="50">
        <Button.Triggers>
            <EventTrigger RoutedEvent="Button.Click">
                <BeginStoryboard Storyboard="{StaticResource MyStoryboard}">
                </BeginStoryboard>
            </EventTrigger>
        </Button.Triggers>
    </Button>
</Grid>

使用纯Xaml可能没有一个好方法。

您可以:

  • 在UserControl上创建一个依赖属性,它接受一个Button,然后你可以绑定它。
  • 制作某种疯狂的转换器,遍历您的可视树,寻找特别的东西。

两者都需要一些C#,所以我不知道上面是否有你想要的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM