只使用XAML从WPF父窗口控制用户控件的故事板 [英] Controlling a UserControl's Storyboard from the parent Window in WPF using only XAML

查看:143
本文介绍了只使用XAML从WPF父窗口控制用户控件的故事板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个非常简单的测试应用程序,以试图解决一个同事向我描述了这个问题。他能够引发它在C#中,但我相信他所需要的解决方案更加通用的,希望它严格的XAML完成。问题是:你如何触发情节提要一个用户控件内响应开始的事件在包含窗口控件(父不必是一个窗口,它可以是一个页面或其他用户控件.. 。我使用一个窗口为简单起见)。所以,我的最好的例子是,你有一个按钮的窗口,你的用户控件的一个实例。您希望按钮的Click事件来触发一个故事板里的用户控件开始。

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.

在用户控件本身,故事板被声明为MyStoryboard的关键资源。我创建了一个按钮与一个EventTrigger表明我可以用一个绑定的前pression引用它引发的故事板。下面是完整的XAML我的用户:

Inside of the UserControl itself, the Storyboard is declared as a Resource with the key of MyStoryboard. I created a Button with an EventTrigger to show that I could trigger the Storyboard by referencing it with a Binding expression. Here is the complete XAML for my UserControl:

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

我希望,因为我可以讲一个故事板,开始与粘合EX pression,我应该能够从包含该用户控件作为子元素一个窗口做同样的事情引用它。但我似乎无法弄清楚如何获得一个引用故事板父窗口内。这是不可能完成的XAML中唯一?

I was hoping that since I was able to tell a Storyboard to begin by referencing it with a Binding expression that I should be able to do the exact same thing from a Window that contains this UserControl as a child element. But I can't seem to figure out how to get a reference to the Storyboard inside of the parent Window. Is this impossible to accomplish in XAML only?

在我的测试项目中的父窗口有什么,但一个按钮,该用户控件的一个实例。如果它甚至有可能,这些元素将让我补充一个EventTrigger那里的事件源是巴顿和触发器行动告诉UserControl的情节提要开始?

The parent window in my test project has nothing but a button and an instance of this UserControl. If it's even possible, which element would let me add an EventTrigger where the source of the event is the Button and the trigger action tells the UserControl's Storyboard to begin?

感谢。

推荐答案

我想回应在用户控件外部事件的想法是不是一个非常安全的。在用户控件,你要只属于控制范围内出生的事件作出反应。

I think the idea of responding to external events within UserControl is not a very safe one. Within UserControl, you'd want to react only to events that are born within the control.

您可以左右翻转的情况,并做到以下几点:?

Can you flip the situation around and do the following: ?

  • 在揭露一个需要动画作为一个依赖属性(背景在这种情况下)的UC属性
  • 父窗口上创建两个EventTrigger中和故事板

这篇关于只使用XAML从WPF父窗口控制用户控件的故事板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆