C# 如何在代码中向自定义控件模板动态添加新的可视状态?
是否可以在代码中以编程方式将新的C# 如何在代码中向自定义控件模板动态添加新的可视状态?,c#,wpf,silverlight,custom-controls,visualstatemanager,C#,Wpf,Silverlight,Custom Controls,Visualstatemanager,是否可以在代码中以编程方式将新的VisualState添加到CustomControl模板的VisualStateManager 例如,我可以在设计时手动将此XAML添加到CustomControl模板: <VisualState x:Name="First"> <Storyboard> <ColorAnimation Duration="0:0:0" Storyboard.TargetName="SBo
VisualState
添加到CustomControl模板的VisualStateManager
例如,我可以在设计时手动将此XAML添加到CustomControl模板:
<VisualState x:Name="First">
<Storyboard>
<ColorAnimation Duration="0:0:0"
Storyboard.TargetName="SBorder"
Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" To="Red" />
</Storyboard>
</VisualState>
storyBoard.Children.Add(animation);
visualState.Storyboard = storyBoard;
visualState.Name = "CoolNameLikeWidthAnimation";
visualStateGroupLookingFor.States.Add(visualState);
但是如何在运行时添加新的
VisualState
?我认为这是可行的,但绝非易事
storyBoard.Children.Add(animation);
visualState.Storyboard = storyBoard;
visualState.Name = "CoolNameLikeWidthAnimation";
visualStateGroupLookingFor.States.Add(visualState);
这应该起作用:
Grid grid = this.Template.FindName("RootElement", this) as Grid;
(VisualStateManager.GetVisualStateGroups(grid)).Add(new VisualStateGroup() { /* the code for your visualstategroup here */ });
storyBoard.Children.Add(animation);
visualState.Storyboard = storyBoard;
visualState.Name = "CoolNameLikeWidthAnimation";
visualStateGroupLookingFor.States.Add(visualState);
(您需要根据模板根元素名称的类型以及设置visualstatemanager的位置进行调整,但所有这些都可以工作
storyBoard.Children.Add(animation);
visualState.Storyboard = storyBoard;
visualState.Name = "CoolNameLikeWidthAnimation";
visualStateGroupLookingFor.States.Add(visualState);
此外,这还添加了一个新的visualStateGroup,而不仅仅是visualState。如果要将visualState添加到现有visualStateGroup,则必须首先从集合中获取该组,但这是常见的“从集合中获取元素”内容
storyBoard.Children.Add(animation);
visualState.Storyboard = storyBoard;
visualState.Name = "CoolNameLikeWidthAnimation";
visualStateGroupLookingFor.States.Add(visualState);
基本上:
storyBoard.Children.Add(animation);
visualState.Storyboard = storyBoard;
visualState.Name = "CoolNameLikeWidthAnimation";
visualStateGroupLookingFor.States.Add(visualState);
VisualStateManager.GetVisualStateGroups()静态方法获取当前visualStateGroups
希望这有帮助。我建议您使用XAML创建组本身
storyBoard.Children.Add(animation);
visualState.Storyboard = storyBoard;
visualState.Name = "CoolNameLikeWidthAnimation";
visualStateGroupLookingFor.States.Add(visualState);
您必须找到您要查找的VisualState组,如下所示:
VisualStateGroup visualStateGroupLookingFor = null;
var visualStateGroups = (VisualStateManager.GetVisualStateGroups(LayoutRoot));
foreach (VisualStateGroup state in visualStateGroups) {
if (state.Name == "VisualStateGroupMine") {
visualStateGroupLookingFor = state;
break;
}
}
storyBoard.Children.Add(animation);
visualState.Storyboard = storyBoard;
visualState.Name = "CoolNameLikeWidthAnimation";
visualStateGroupLookingFor.States.Add(visualState);
然后,您必须创建一个新的VisualState和故事板来添加,例如:
var visualState = new VisualState();
var storyBoard = new Storyboard();
storyBoard.Children.Add(animation);
visualState.Storyboard = storyBoard;
visualState.Name = "CoolNameLikeWidthAnimation";
visualStateGroupLookingFor.States.Add(visualState);
现在,创建动画:
var animation = new DoubleAnimation();
animation.To = 10.0;
//assuming this is instance of class ClassFoo
//and you want to animate it's Width
Storyboard.SetTarget(animation, this);
Storyboard.SetTargetProperty(animation, new PropertyPath(ClassFoo.WidthProperty));
storyBoard.Children.Add(animation);
visualState.Storyboard = storyBoard;
visualState.Name = "CoolNameLikeWidthAnimation";
visualStateGroupLookingFor.States.Add(visualState);
并设置动画的目标:
var animation = new DoubleAnimation();
animation.To = 10.0;
//assuming this is instance of class ClassFoo
//and you want to animate it's Width
Storyboard.SetTarget(animation, this);
Storyboard.SetTargetProperty(animation, new PropertyPath(ClassFoo.WidthProperty));
storyBoard.Children.Add(animation);
visualState.Storyboard = storyBoard;
visualState.Name = "CoolNameLikeWidthAnimation";
visualStateGroupLookingFor.States.Add(visualState);
最后,将动画添加到情节提要,为其命名,并将其添加到VisualState组:
storyBoard.Children.Add(animation);
visualState.Storyboard = storyBoard;
visualState.Name = "CoolNameLikeWidthAnimation";
visualStateGroupLookingFor.States.Add(visualState);
就这样,像往常一样用
storyBoard.Children.Add(animation);
visualState.Storyboard = storyBoard;
visualState.Name = "CoolNameLikeWidthAnimation";
visualStateGroupLookingFor.States.Add(visualState);
VisualStateManager.GoToState(this, "CoolNameLikeWidthAnimation", true);