从VisualState中查找指定名称的动画 并启用

从VisualState中查找指定名称的动画

/// <summary>
/// 从VisualState中查找指定名称的动画
/// </summary>
/// <param name="control">控件对象</param>
/// <param name="childName">模板中的子控件名称</param>
/// <param name="transitionName">Transition的名称</param>
/// <returns>动画</returns>
public static Storyboard FindStorybardInVisualState(this Control control, FrameworkElement childControl, string transitionName)
{
Collection<VisualStateGroup> grps = (Collection<VisualStateGroup>)VisualStateManager.GetVisualStateGroups(childControl);
foreach (VisualStateGroup grp in grps)
{
Collection<VisualState> states = (Collection<VisualState>)grp.States;
foreach (VisualState state in states)
{
if (state.Name == transitionName)
{
return state.Storyboard;
}
}
}
return null;
}

Resource文件

<Style TargetType="local:TransitioningFrame">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:TransitioningFrame">
<Border HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
x:Name="brdRoot">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="PresentationStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="Transition">
<Storyboard FillBehavior="Stop"
x:Name="stbPlaneProjectionTransition">
<!--具体动画实现方式-->

</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid>
<ContentPresenter x:Name="CurrentContentPresentationSite"
Content="{x:Null}"
ContentTemplate="{TemplateBinding ContentTemplate}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />

</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

调用方式:

private Storyboard m_PlaneProjectionTransitionStoryboard;

public override void OnApplyTemplate()
{
base.OnApplyTemplate();

Border rootBorder = GetTemplateChild("brdRoot") as Border;

m_PlaneProjectionTransitionStoryboard =

this.FindStorybardInVisualState(rootBorder, "Transition");

}

启用Visual中的状态动画

VisualStateManager.GoToState(this, "Transition", true);

posted on 2011-08-26 14:05  温文粥  阅读(216)  评论(0编辑  收藏  举报

导航