如何在加载元素时运行特定的 GoToStateAction-with-DataTrigger

发布于 2024-09-13 08:18:20 字数 271 浏览 3 评论 0原文

我有一个数据模板。它有两种视觉状态 - 展开、折叠。 我添加了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

简单气质女生网名 2024-09-20 08:18:20

听起来您需要重写 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.

赴月观长安 2024-09-20 08:18:20

您只需添加另一个 GoToStateAction 即可在 OnLoad 事件触发时设置所需的状态。

更新

我还没有对此进行测试,但我认为您可以使用派生自 GoToStateAction 的自定义 TargetedTriggerAction

public class GoToStateIfCheckedAction : GoToStateAction
{
    protected override void Invoke(object parameter)
    {
        var toggleButton = Target as ToggleButton;
        if (toggleButton != null && (!toggleButton.IsChecked.HasValue || !toggleButton.IsChecked.Value))
        {
            // if the Target is a ToggleButton, and it is in an indeterminate or unchecked state, don't invoke
            return;
        }

        // if the Target is not a ToggleButton, or if the ToggleButton is checked, go ahead and invoke the action
        base.Invoke(parameter);
    }
}

当附加到 ToggleButton 时,例如如CheckBox,只有当IsChecked == true时才会执行此操作。

您可以从 OnLoad 事件触发此事件,如果选中该框,它将进入该状态,如果未选中,则不执行任何操作。

You just need to add another GoToStateAction that sets the desired states upon the OnLoad event firing.

update

I haven't tested this, but I think you could use a custom TargetedTriggerAction that derives from GoToStateAction:

public class GoToStateIfCheckedAction : GoToStateAction
{
    protected override void Invoke(object parameter)
    {
        var toggleButton = Target as ToggleButton;
        if (toggleButton != null && (!toggleButton.IsChecked.HasValue || !toggleButton.IsChecked.Value))
        {
            // if the Target is a ToggleButton, and it is in an indeterminate or unchecked state, don't invoke
            return;
        }

        // if the Target is not a ToggleButton, or if the ToggleButton is checked, go ahead and invoke the action
        base.Invoke(parameter);
    }
}

When attached to a ToggleButton, such as CheckBox, this action will only be executed when IsChecked == 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.

不…忘初心 2024-09-20 08:18:20

我有一个类似的问题,即绑定的视觉状态未应用于视图加载:

<core:PropertyChangedTrigger Binding="{Binding State}">
    <core:GoToStateAction StateName="{Binding State}" />
</core:PropertyChangedTrigger>

由于我使用 MVVM 架构,因此我无法覆盖视图的 OnApplyTemplate,因为视图无法“查看”ViewModel。

最后,我发现 EventTrigger 有帮助,我想与您分享:

<interactivity:Interaction.Triggers>
    <interactivity:EventTrigger>
        <core:GoToStateAction StateName="{Binding State}" />
    </interactivity:EventTrigger>
    <core:PropertyChangedTrigger Binding="{Binding State}">
        <core:GoToStateAction StateName="{Binding State}" />
    </core:PropertyChangedTrigger>
</interactivity:Interaction.Triggers>

其中 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:

<core:PropertyChangedTrigger Binding="{Binding State}">
    <core:GoToStateAction StateName="{Binding State}" />
</core:PropertyChangedTrigger>

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:

<interactivity:Interaction.Triggers>
    <interactivity:EventTrigger>
        <core:GoToStateAction StateName="{Binding State}" />
    </interactivity:EventTrigger>
    <core:PropertyChangedTrigger Binding="{Binding State}">
        <core:GoToStateAction StateName="{Binding State}" />
    </core:PropertyChangedTrigger>
</interactivity:Interaction.Triggers>

where xmlns:interactivity="http://schemas.microsoft.com/expression/2010/interactivity"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文