Animation 操纵故事板';s目标对象
在Animation 操纵故事板';s目标对象,animation,silverlight-3.0,storyboard,Animation,Silverlight 3.0,Storyboard,在情节提要的已完成事件的处理程序中,如何获取应用情节提要的元素 我的故事板是ItemTemplate的一部分: <ListBox x:Name="MyListBox" > <ListBox.ItemTemplate> <DataTemplate> <Grid x:Name="Container" Height="30" > <Grid.Resources>
情节提要
的已完成
事件的处理程序中,如何获取应用情节提要的元素
我的故事板是ItemTemplate的一部分:
<ListBox x:Name="MyListBox" >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid x:Name="Container" Height="30" >
<Grid.Resources>
<Storyboard x:Name="FadeOut" BeginTime="0:0:7" Completed="FadeOut_Completed">
<DoubleAnimation From="1.0" To="0.0" Duration="0:0:3" Storyboard.TargetName="Container" Storyboard.TargetProperty="Opacity" />
</Storyboard>
</Grid.Resources>
[...snip...]
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
[…剪断…]
在Completed
事件中,我想抓取名为Container的网格,这样我就可以用它的DataContext做一些不愉快的事情。这是可以做到的,还是我走错了方向
谢谢:)答案是这是不可能的-无论如何在Silverlight 3中都不可能 使用调试器,我能够找到故事板的私有属性,当我沿着对象树走到包含的模板项时,我找到了该属性-但是,由于silverlight应用程序上的安全限制,我无法使用反射从代码中触及该属性(尽管在WPF中这很可能) 我最终的解决方案是使用一个
字典
,和两个事件处理程序。对于我附加了一个Loaded
处理程序的模板,这意味着每次创建和加载模板实例时(即,对于绑定到列表框的每个数据项),都会调用我的处理程序。此时,我有一个对模板的物理实例的引用,因此我可以在其子级中搜索故事板:
private void ItemTemplate_Loaded(object sender, RoutedEventArgs e)
{
Storyboard s = getStoryBoard(sender);
if (s != null)
{
if (!_startedStoryboards.ContainsKey(s))
_startedStoryboards.Add(s, (Grid)sender);
}
}
private Storyboard getStoryBoard(object container)
{
Grid g = container as Grid;
if (g != null)
{
if (g.Resources.Contains("FadeOut"))
{
Storyboard s = g.Resources["FadeOut"] as Storyboard;
return s;
}
}
return null;
}
private Dictionary<Storyboard, Grid> _startedStoryboards = new Dictionary<Storyboard, Grid>();
[注:此代码已消毒,供公众查看,请原谅您可能发现的任何小差异或语法错误]
private void FadeOut_Completed(object sender, EventArgs e)
{
if (_startedStoryboards.ContainsKey((Storyboard)sender))
{
Grid g = _startedStoryboards[(Storyboard)sender];
if (g.DataContext != null)
{
MyDataItem z = g.DataContext as MyDataItem;
if (z != null)
{
... do my thing ...
}
}
}
}