如何在代码中动态地将新的视觉状态添加到自定义控件模板?
是否可以在代码中以编程方式将新的 VisualState
添加到 CustomControl Template 的 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>
但是如何在运行时添加新的 VisualState
呢?
Is it possible to add a new VisualState
to a CustomControl Template's VisualStateManager
programmatically in code?
For example, I can add to a CustomControl Template this XAML manually in design-time:
<VisualState x:Name="First">
<Storyboard>
<ColorAnimation Duration="0:0:0"
Storyboard.TargetName="SBorder"
Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" To="Red" />
</Storyboard>
</VisualState>
But how could I add a new VisualState
in runtime?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这是可行的,但绝不是容易的...
这应该可行:(
您需要根据模板根元素名称的类型以及设置 VisualStateManager 的位置进行调整,但全部都在所有这些都可以工作
,这会添加一个新的 VisualStateGroup,而不仅仅是一个 VisualState 如果要将 VisualState 添加到现有的 VisualStateGroup,则必须首先从集合中获取该组,但这是常见的“获取元素”。 使用
基本上:
VisualStateManager.GetVisualStateGroups()
静态方法获取当前的 VisualStateGroups希望这有帮助。
I think this is doable, but by no means easy...
this should work:
(you'll need to adapt depending on the type of your template's root element's name, and the place where you set-up the visualstatemanager, but all in all this can work.
also, this adds a new visualStateGroup, not just a visualState. If you want to add a VisualState to an existing visualStateGroup, you'll have to get the group from the collection first, but this is common "get element from a collection" stuff
basically:
VisualStateManager.GetVisualStateGroups()
static method to get the current visualStateGroupshope this helps.
我建议您应该使用 XAML 创建组本身,然后
你必须像这样找到你正在寻找的 VisualStateGroup:
然后,你必须创建一个新的 VisualState 和 Storyboard 来添加,例如:
现在,创建动画:
并设置动画的目标:
最后添加动画)到你的故事板,给它一个名字,将它添加到视觉状态组:
就是这样,像往常一样触发它
You should create the Group itself using XAML I suggest, then
you have to find the VisualStateGroup you are Looking for like this:
Then, you have to create a new VisualState and Storyboard to add, for example:
Now, create the animation:
And set the target of the animation:
Finally add the animation(s) to your storyboard, give it a name, add it to the visualstategroup:
Thats it, trigger it as usual with