PHEW.
I'm serious. I'll spell it out as follows...
The Storyboard has a Key "myStoryboard". It's held in a DataTemplate with a key "myDataTemplate".
This data template is used in a ContentControl with the name "myContentControl" by this tag:
<ContentControl Name="myContentControl"
ContentTemplate="{DynamicResource myDataTemplate}"/>
The content control is used in my UserControl. In the UserControl codebehind I've made a keyboard gesture that supposed to start "myStoryBoard" but i'm having no such luck getting to it.
private void StartSB(object sender, ExecutedRoutedEventArgs e)
{
Storyboard sb = (Storyboard) this.TryFindResource("myStoryboard");
sb.Begin(this);
}
sb is always null here. How can i get the Storyboard?
UPDATE:
so playing with TryFindResource() I've managed to get to myDataTemplate
private void StartSB(object sender, ExecutedRoutedEventArgs e)
{
object sb = this.myContentControl.TryFindResource("myDataTemplate");
}
in the Locals viewer I can see sb is myDataTemplate. I can see in the tree sb.base.Resources.Keys which is an array of resources in which "myStoryboard" is inside. Oh so close!
UPDATE2:
More code complete here. I realize now this may be too spaghettified to explain with words.
<UserControl >
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources\myUCResources.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<ContentControl Name="myContentControl"
ContentTemplate="{DynamicResource myDataTemplate}"
Content="{Binding}" />
...
</UserControl>
now the codebehind for this User control
namespace myclass
{
public partial class myUserControl: UserControl, System.ComponentModel.INotifyPropertyChanged
{
...
public myUserControl()
{
InitializeComponent();
<!--setup keybinding-->
}
public void KeyBindExecuted(object sender, ExecutedRoutedEventArgs e)
{
Object sb = this.myContentControl.TryFindResource("myDataTemplate");
//sb returns the DataTemplate
}
}
}
And finally the resource dictionary containing the UI stuff and the animation I ultimately want to trigger. (myUCResources.xaml)
<ResourceDictionary>
<DataTemplate x:Key="myDataTemplate" >
<Grid>
<!-- elements like buttons -->
</Grid>
<DataTemplate.Resources>
<Storyboard x:Key="myStoryBoard">
<DoubleAnimation>
<!-- animation stuff-->
</DoubleAnimation>
</Storyboard>
</DataTemplate.Resources>
<DataTemplate.Triggers>
<EventTrigger SourceName="aButton" RoutedEvent="Button.Click">
<BeginStoryboard Storyboard="{StaticResource myStoryBoard}" />
</EventTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ResourceDictionary>
Update 3:
ok different approach. Can I use the EventTrigger in the DataTemplate from the codebehind to start the animation?