简体   繁体   中英

How do I start a Storyboard in a Data Template in a Content Control in a User Control from codebehind?

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?

AH HAH!

So I found a roundabout way to solving this. My third update where I entertained the thought of just firing an event seemed more fruitful. All can be found here.

http://www.codeproject.com/script/Forums/View.aspx?fid=1004114&msg=2827455

In a nutshell I used FindResource to get the DataTemplate, then FindName of the button in the DataTemplate used to normally trigger the animation. Then I raised a button click on that button.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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