在wpf MVVM中处理窗口关闭按钮

And*_*ayi 19 wpf mvvm

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

Har*_*tha 33

有几种方法可以做到这一点.我已经指出了以下两种方法.

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

  2. 您可以使用以下代码

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>
Run Code Online (Sandbox Code Playgroud)

注意:添加System.Windows.Interactivity引用

查看模型

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
}
Run Code Online (Sandbox Code Playgroud)

这是我的RelayCommand类.

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;
}
Run Code Online (Sandbox Code Playgroud)

  • 好的,`Closing` 是在 **window 关闭** 之前触发的 `Window` 事件,它不等于 **X 按钮 Click**,因为 `Window` 可以在代码中的任何地方被 `Window` 关闭。关闭()`。因此,如果需要将通过 UI 关闭的窗口与通过代码关闭(例如 - 一种自定义 InputDialog)分开 - 这是非常核心的,我现在看不到任何解决方案。 (2认同)