I created a very simple test application to try to solve this problem, described to me by an employee. He was able to call it in C #, but I believe that he needs the solution to be more general and require strict implementation of XAML. The problem was this: how do you start the Storyboard inside the UserControl to start in response to an event in the control in the containing window (the parent element does not have to be a window, it can be a page or another UserControl ... I use Window for simplicity). So, my best example: you have a window with a button and an instance of your UserControl. You want the button's Click event to trigger the storyboard inside the UserControl to begin with.
Inside the UserControl itself, the storyboard is declared as a resource using the MyStoryboard key. I created a button with EventTrigger to show that I can start the storyboard by referencing it using the Binding expression. Here is the full 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>
I was hoping that since I was able to tell the storyboards by navigating to it using the Binding expression, I would have to do the same from the window containing this UserControl as a child. But I can’t figure out how to get the storyboard link inside the parent window. Is this impossible to do only in XAML?
UserControl. , EventTrigger, Button, , UserControl?
.