如何在加载元素时运行特定的 GoToStateAction-with-DataTrigger
我有一个数据模板。它有两种视觉状态 - 展开、折叠。 我添加了 2 个 GoToStateAction-s。当数据上下文属性变为 True 时,第一个状态进入 Expanded 状态,而当同一属性变为 False 时,第二个状态进入 Collapsed 状态。
复选框是模板的一部分并绑定到该属性。因此,当复选框被选中/取消选中时,就会发生必要的转换。
但启动时不会应用任何操作。复选框已选中,但未应用扩展视觉状态。
是否可以使用视觉状态管理器让所有项目加载根据属性值应用的状态?
I have a DataTemplate. It has two visual states - Expanded, Collapsed.
I added 2 GoToStateAction-s. The first one goes to the Expanded state when a data context property becomes True, and the second one goes to the Collapsed state when that same property becomes False.
A Checkbox is part of the template and bound to that property. So when the Checkbox gets checked/unchecked the necessary transition happens.
But none of the actions are applied on startup. The Checkbox is Checked but the Expanded visual state is not applied.
Is it possible using the Visual State Manager to have all items loaded with states applied according to the property values?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听起来您需要重写 OnApplyTemplate 并调用 VisualStateManager.GoToState()。所发生的情况是您的控件已加载,数据绑定发生,然后应用了模板。因此,模板处于基本状态,因为没有任何东西告诉它执行状态转换。您也许可以通过挂钩加载的事件从 XAML 完成这一切,但您可能会发现它很不稳定。
It sounds like you need to override OnApplyTemplate and call VisualStateManager.GoToState(). What happened was your control was loaded, data binding occurred, and then the template was applied. Thus the template is in the base state because nothing told it to perform a state transition. You might be able to do it all from XAML by hooking into the loaded event, but you might find it to be flaky.
您只需添加另一个
GoToStateAction
即可在OnLoad
事件触发时设置所需的状态。更新
我还没有对此进行测试,但我认为您可以使用派生自
GoToStateAction
的自定义TargetedTriggerAction
:当附加到
ToggleButton
时,例如如CheckBox
,只有当IsChecked == true
时才会执行此操作。您可以从
OnLoad
事件触发此事件,如果选中该框,它将进入该状态,如果未选中,则不执行任何操作。You just need to add another
GoToStateAction
that sets the desired states upon theOnLoad
event firing.update
I haven't tested this, but I think you could use a custom
TargetedTriggerAction
that derives fromGoToStateAction
:When attached to a
ToggleButton
, such asCheckBox
, this action will only be executed whenIsChecked == true
.You could trigger this from the
OnLoad
event and it will go to the state if the box is checked, or do nothing if unchecked.我有一个类似的问题,即绑定的视觉状态未应用于视图加载:
由于我使用 MVVM 架构,因此我无法覆盖视图的 OnApplyTemplate,因为视图无法“查看”ViewModel。
最后,我发现 EventTrigger 有帮助,我想与您分享:
其中 xmlns:interactivity="http://schemas.microsoft.com/expression/2010/interactivity"
I have a similar problem that a binded visual state is not applied on view load:
As I am using MVVM architecture I cannot override the view's OnApplyTemplate as the view cannot 'see' the ViewModel.
Finally, I found that EventTrigger helps and I want to share it with you:
where xmlns:interactivity="http://schemas.microsoft.com/expression/2010/interactivity"