【问题标题】:handling window close button in wpf MVVM处理wpf MVVM中的窗口关闭按钮
【发布时间】:2013-03-04 12:02:29
【问题描述】:

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

【问题讨论】:

标签: wpf mvvm


【解决方案1】:

有几种方法可以做到这一点。我在下面指出了两种方法。

  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>

注意:添加 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
}

这是我的 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;
}

【讨论】:

  • 就是这样,MVVM Light可以帮到你!
  • thanx haritha,我试过了,但它对我不起作用,我找不到 'system.windows.interactivity' 命名空间..我正在使用 VS2010.. 有人建议使用 'windowsbase' 但即便如此无法让 xmlns:i="schemas.microsoft.com/expression/2010/interactivity" 工作...
  • 嗨 Andris,您必须安装一个可再发行组件才能在 Visual Studio 中显示它。你可以下载它here
  • OK,ClosingWindow关闭窗口之前触发的事件,不等于X按钮点击,因为@ 987654328@ 可以通过Window.Close() 从代码中的任何位置关闭。因此,如果需要将通过 UI 关闭的窗口与通过代码关闭(例如 - 一种自定义 InputDialog)区分开来 - 那是非常核心的,我现在看不到任何解决方案。
  • Expression SDK Install-Package Expression.Blend.Sdk 有一个 nuget 包,以防其他人因缺少 .dll 而遇到介绍性问题
【解决方案2】:

问题是我正在关闭父窗口并在关闭其各自的子窗口后重新打开它,导致内存泄漏。我通过隐藏父窗口然后在子窗口关闭后再次显示它来解决。我是 wpf 和 windows 开发的新手,所以我边走边学。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-04
    • 1970-01-01
    • 1970-01-01
    • 2016-09-04
    • 1970-01-01
    相关资源
    最近更新 更多