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

Ric*_*ich 5 .net c# wpf xaml

I have created a very simple test app to try and solve this problem described to me by a co-worker. He was able to trigger it in C#, but I believe he needed the solution to be more generic and wanted it to be done strictly in the XAML. The problem was: How do you trigger the Storyboard inside of a UserControl to begin in response to an event on a control in the containing Window (the parent doesn't have to be a Window, it can be a Page or another UserControl...I use a Window for simplicity). So, my best example is that you have a Window with a Button and an instance of your UserControl. You want the Click event of the Button to trigger a Storyboard inside of the UserControl to begin.

在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>
Run Code Online (Sandbox Code Playgroud)

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

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

谢谢.

小智 1

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

<BeginStoryboard Storyboard="{StaticResource MyStoryboard}">
</BeginStoryboard>
Run Code Online (Sandbox Code Playgroud)

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

    <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>
Run Code Online (Sandbox Code Playgroud)