使用交互触发器调用可见性更改方法 WPF [英] Using interaction trigger to call visibility changed method WPF

查看:29
本文介绍了使用交互触发器调用可见性更改方法 WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚的是两件事,如何在用户控件的可见性更改时触发触发器,以及如何将可见性的值作为参数传递.

What I would like to figure out is two things, how to get a trigger occurring when a user control's visibility is changed and passing the value of visibility through as a parameter.

无论出于何种原因,触发器似乎都没有触发.我刚刚添加了 ControlVisible 参数来显示我想要发生的事情,在测试时它并不存在,并且只有一个消息框可以在可见性改变时捕获,如注释掉的方法.

For whatever reason the trigger doesn't seem to be firing. I have only just added in the ControlVisible parameter to show what I would like to happen, when testing it was not there and just had a messagebox inside to catch when visibility changed, as in the commented out method.

我在 Visual Studio 2010 中使用 4.0

I am using 4.0 with Visual Studio 2010

包含用户控件的主窗口视图

Main Window View which contains the user control

<Window x:Class="bt.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:vm="clr-namespace:bt"
        xmlns:ctrls="clr-namespace:bt.Controls"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        xmlns:ei="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions"
        mc:Ignorable="d">

    <Grid>
        <ctrls:Login Visibility="{Binding DataContext.Vis,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window},Converter={StaticResource BooleanToVisibilityConverter}}" >
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="IsVisibleChanged">
                    <ei:CallMethodAction MethodName="VisibleTrigger" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </ctrls:Login>
    </Grid>
</Window>

用户控件视图模型:

namespace bt.Controls
{
    class LoginViewModel
    {
        public LoginViewModel()
        {
        }

        public void VisibleTrigger(bool ControlVisible)
        {
            if (ControlVisible)
            {
                MessageBox.Show("Start timer");
            }
            else
            {
                MessageBox.Show("Stop timer");
            }
        }

        //public void VisibleTrigger()
        //{
        //    MessageBox.Show("Changed");
        //}
    }
}

推荐答案

首先,我们需要设置TargetObject 属性到视图模型/数据上下文,因为要调用的方法在视图模型中可用:

First, we need to set TargetObject property to viewmodel/DataContext, because method to be invoked is available in the viewmodel :

......
<i:Interaction.Triggers>
    <i:EventTrigger EventName="IsVisibleChanged">
        <ei:CallMethodAction MethodName="VisibleTrigger" TargetObject="{Binding}"/>
    </i:EventTrigger>
</i:Interaction.Triggers>
......

其次,EventTrigger 似乎不适用于 IsVisibleChanged 事件.所以上面的代码片段适用于其他事件,但不适用于 IsVisibleChanged.我们可以在 this SO question的答案中找到一种解决方法,方法是使用 PropertyChangedTrigger 来监听Visibility 属性已更改,而不是侦听 IsVisibleChanged 事件:

Second, EventTrigger doesn't seems to work specifically with IsVisibleChanged event. So code snippet above works for other event, but not IsVisibleChanged. We can find a workaround in the answer to this SO question, by using PropertyChangedTrigger to listen to Visibility property changed, instead of listening to IsVisibleChanged event :

<i:Interaction.Triggers>
    <ei:PropertyChangedTrigger Binding="{Binding Visibility, ElementName=MyControlName}">
        <ei:CallMethodAction  MethodName="VisibleTrigger" TargetObject="{Binding}"/>
    </ei:PropertyChangedTrigger>
</i:Interaction.Triggers>

第三,CallMethodAction 似乎没有提供将参数传递给方法的方法.为了能够调用带有参数的方法,我们最好按照建议使用 InvokeCommandAction 而不是 CallMethodAction 此处 以及 @Rohit 的建议> 在您的上一个问题中.

Third, CallMethodAction doesn't seems to provide a way to pass parameter to the method. To be able to invoke a method with parameter we better use InvokeCommandAction instead of CallMethodAction as suggested here and also suggested by @Rohit in your previous question.

这篇关于使用交互触发器调用可见性更改方法 WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆