你应该使用XAML创建组本身,然后按照以下方式查找你要查找的VisualStateGroup:
VisualStateGroup visualStateGroupLookingFor = null;
var visualStateGroups = (VisualStateManager.GetVisualStateGroups(LayoutRoot));
foreach (VisualStateGroup state in visualStateGroups) {
if (state.Name == "VisualStateGroupMine") {
visualStateGroupLookingFor = state;
break;
}
}
然后,您需要创建一个新的VisualState和Storyboard来添加,例如:
var visualState = new VisualState();
var storyBoard = new Storyboard();
现在,创建动画:
var animation = new DoubleAnimation();
animation.To = 10.0;
并设置动画的目标:
Storyboard.SetTarget(animation, this);
Storyboard.SetTargetProperty(animation, new PropertyPath(ClassFoo.WidthProperty));
最后将动画添加到你的故事板中,给它一个名称,并将其添加到visualstategroup中:
storyBoard.Children.Add(animation)
visualState.Storyboard = storyBoard
visualState.Name = "CoolNameLikeWidthAnimation"
visualStateGroupLookingFor.States.Add(visualState)
就这样,像往常一样触发它
VisualStateManager.GoToState(this, "CoolNameLikeWidthAnimation", true)