[英]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". 故事板有一个键“myStoryboard”。 It's held in a DataTemplate with a key "myDataTemplate".
它保存在一个带有键“myDataTemplate”的DataTemplate中。
This data template is used in a ContentControl with the name "myContentControl" by this tag: 此数据模板在ContentControl中使用,此标记名称为“myContentControl”:
<ContentControl Name="myContentControl"
ContentTemplate="{DynamicResource myDataTemplate}"/>
The content control is used in my UserControl. 内容控件在我的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.
在UserControl代码隐藏中,我做了一个键盘手势,应该启动“myStoryBoard”,但我没有这样的运气。
private void StartSB(object sender, ExecutedRoutedEventArgs e)
{
Storyboard sb = (Storyboard) this.TryFindResource("myStoryboard");
sb.Begin(this);
}
sb is always null here. sb在这里总是为空。 How can i get the Storyboard?
我怎样才能获得故事板?
UPDATE: 更新:
so playing with TryFindResource() I've managed to get to myDataTemplate 所以玩TryFindResource()我已经设法进入myDataTemplate
private void StartSB(object sender, ExecutedRoutedEventArgs e)
{
object sb = this.myContentControl.TryFindResource("myDataTemplate");
}
in the Locals viewer I can see sb is myDataTemplate. 在Locals查看器中,我可以看到sb是myDataTemplate。 I can see in the tree sb.base.Resources.Keys which is an array of resources in which "myStoryboard" is inside.
我可以在树中看到sb.base.Resources.Keys,这是一个“myStoryboard”所在的资源数组。 Oh so close!
哦,这么近!
UPDATE2: 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. 最后是包含UI内容和我最终想要触发的动画的资源字典。 (myUCResources.xaml)
(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: 更新3:
ok different approach. 确定不同的方法。 Can I use the EventTrigger in the DataTemplate from the codebehind to start the animation?
我可以从代码隐藏中使用DataTemplate中的EventTrigger来启动动画吗?
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 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. 简而言之,我使用FindResource获取DataTemplate,然后使用DataTemplate中的按钮的FindName来正常触发动画。 Then I raised a button click on that button.
然后我按下了那个按钮的按钮。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.