2013-03-04 10 views
18

Czy istnieje sposób na obsłużenie przycisku zamykania okna, np. "X" w prawym górnym rogu w modelu podglądu przez powiązanie z poleceniem? lub przesłonięcie polecenia window.close, aby zamknięcie jednego okna powróciło do poprzedniego okna. Thanx.Przycisk zamykania okna obsługi w WPF MVVM

Odpowiedz

30

Istnieje kilka metod. Podkreśliłem dwie metody poniżej.

  1. Za pomocą dołączonych poleceń można powiązać przycisk zamykania w modelu widoku.

  2. można użyć poniższy kod

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> 

UWAGA: Dodaj odniesienie System.Windows.Interactivity

Widok Model

To jest moja klasa 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; 
} 
+0

Dokładnie tak, MVVM Light może pomóc! –

+0

thanx haritha, próbowałem go, ale to nie działało dla mnie, nie mogłem znaleźć przestrzeni nazw system.windows.interactivity..używałem VS2010..Niektóre sugerowały 'windowsbase', ale nawet to nie działało, aby uzyskać xmlns: i = "http://schemas.microsoft.com/expression/2010/interactivity" do pracy ... –

+1

Cześć Andris, Musisz zainstalować redystrybucyjną, aby pojawiła się w Visual Studio. Możesz go pobrać [tutaj] (http://www.microsoft.com/en-us/download/details.aspx?id=10801). – Haritha

0

Problem polegał na tym, że zamykałem okno nadrzędne i ponownie otwierałem je po zamknięciu odpowiedniego okna podrzędnego, powodując wycieki pamięci. Rozwiązałem, ukrywając okno nadrzędne, a następnie pokazując je ponownie po zamknięciu okna podrzędnego. Jestem nowy w tworzeniu wpf i windowsów, więc uczę się jak idę.

Powiązane problemy