处理 wpf MVVM 中的窗口关闭按钮 [英] handling window close button in wpf MVVM

查看:35
本文介绍了处理 wpf MVVM 中的窗口关闭按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法通过绑定到命令来处理视图模型右上角的窗口关闭按钮,即X"?或覆盖 window.close 命令,以便关闭一个窗口回到上一个窗口.谢谢.

解决方案

有几种方法可以解决这个问题.我在下面指出了两种方法.

  1. 您可以使用附加命令绑定视图模型中的关闭按钮.

  2. 您可以使用下面的代码

XML:

<i:Interaction.Triggers><i:EventTrigger EventName="关闭"><i:InvokeCommandAction Command="{Binding CloseWindowCommand}"/></i:EventTrigger></i:Interaction.Triggers><网格></网格></窗口>

<块引用>

注意:添加 System.Windows.Interactivity 参考

查看模型

私有 ICommand closeWindowCommand;公共 ICommand CloseWindowCommand{得到{if (closeWindowCommand == null){closeWindowCommand = new RelayCommand(param => this.CloseWindow(), null);}返回关闭窗口命令;}}私有无效 CloseWindow(){//做你的操作}

这是我的 RelayCommand 类.

public class RelayCommand : ICommand{///<总结>///初始化 <see cref="RelayCommand"/> 的新实例班级.///</总结>///<param name="execute">执行.</param>公共中继命令(动作<对象>执行):这个(执行,空){}///<总结>///初始化 <see cref="RelayCommand"/> 的新实例班级.///</总结>///<param name="execute">执行.</param>///<param name="canExecute">可以执行.</param>public RelayCommand(Action<object> execute, Predicate<object> canExecute){如果(执行 == 空)throw new ArgumentNullException("execute");_execute = 执行;_canExecute = canExecute;}///<总结>///定义判断命令在当前状态下是否可以执行的方法.///</总结>/// 命令使用的数据.如果命令不需要传递数据,则可以将此对象设置为 null.///<返回>///如果可以执行此命令,则为真;否则为假.///</returns>public bool CanExecute(对象参数){返回 _canExecute == null ?真:_canExecute(参数);}///<总结>///当发生影响命令是否应执行的更改时发生.///</总结>公共事件 EventHandler CanExecuteChanged{添加 { CommandManager.RequerySuggested += value;}删除 { CommandManager.RequerySuggested -= value;}}///<总结>///定义调用命令时要调用的方法.///</总结>/// 命令使用的数据.如果命令不需要传递数据,则可以将此对象设置为 null.公共无效执行(对象参数){_执行(参数);}///<总结>///行动///</总结>私有只读操作<对象>_执行;///<总结>///谓词///</总结>private readonly Predicate<object>_canExecute;}

is there a way to handle the window close button ie "X" in the top right corner in the viewmodel by binding to a command? or overriding the window.close command so that closing one window goes back to the previous window. Thanx.

解决方案

There are several methods for this. I have pointed out two methods below.

  1. You can use attached commands to bind the close button in your view model.

  2. You can use Below code

Xaml:

<Window x:Class="WpfInfragisticsModal.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" 
        xmlns:ig="http://schemas.infragistics.com/xaml"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        Name="myWindow">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Closing">
            <i:InvokeCommandAction Command="{Binding CloseWindowCommand}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <Grid>
    </Grid>
</Window>

NOTE: Add System.Windows.Interactivity reference

View Model

private ICommand closeWindowCommand;

public ICommand CloseWindowCommand
{
      get
      {
          if (closeWindowCommand == null)
          {
             closeWindowCommand = new RelayCommand(param => this.CloseWindow(), null);
          }
          return closeWindowCommand;
      }
 }

private void CloseWindow()
{
     //Do your operations
}

This is my RelayCommand class.

public class RelayCommand : ICommand
{
    /// <summary>
    /// Initializes a new instance of the <see cref="RelayCommand"/> class.
    /// </summary>
    /// <param name="execute">The execute.</param>
    public RelayCommand(Action<object> execute)
        : this(execute, null)
    {
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="RelayCommand"/> class.
    /// </summary>
    /// <param name="execute">The execute.</param>
    /// <param name="canExecute">The can execute.</param>
    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
            throw new ArgumentNullException("execute");
        _execute = execute;
        _canExecute = canExecute;
    }

    /// <summary>
    /// Defines the method that determines whether the command can execute in its current state.
    /// </summary>
    /// <param name="parameter">Data used by the command.  If the command does not require data to be passed, this object can be set to null.</param>
    /// <returns>
    /// true if this command can be executed; otherwise, false.
    /// </returns>
    public bool CanExecute(object parameter)
    {
        return _canExecute == null ? true : _canExecute(parameter);
    }

    /// <summary>
    /// Occurs when changes occur that affect whether or not the command should execute.
    /// </summary>
    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    /// <summary>
    /// Defines the method to be called when the command is invoked.
    /// </summary>
    /// <param name="parameter">Data used by the command.  If the command does not require data to be passed, this object can be set to null.</param>
    public void Execute(object parameter)
    {
        _execute(parameter);
    }

    /// <summary>
    /// Action
    /// </summary>
    private readonly Action<object> _execute;


    /// <summary>
    /// Predicate
    /// </summary>
    private readonly Predicate<object> _canExecute;
}

这篇关于处理 wpf MVVM 中的窗口关闭按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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