2010-05-24 20 views
0

Ma fenêtre de maintenance WPF comporte une barre d'outils avec un bouton "Quitter"; un CommandExit est lié avec ce bouton. La commande CommandExit effectue des vérifications avant la sortie.Commande Lier au bouton X de la barre de titre de la fenêtre

Maintenant, si je clique pour fermer le bouton de la fenêtre (bouton x de la barre de titre), ces contrôles sont ignorés.

Comment puis-je lier CommandExit à la fenêtre x-button?

Répondre

0

Vous devez mettre en œuvre gestionnaire d'événements de votre principal événement de la fenêtre « Clôture » où vous pouvez faire des vérifications et annuler l'action de fermeture . C'est le moyen le plus facile de le faire, mais sinon, vous devez redessiner toute la fenêtre et son thème.

6

Je suppose que vous pouvez vouloir annuler la fermeture en fonction de ces conditions? Vous devez utiliser le Closing event, qui vous passe un System.ComponentModel.CancelEventArgs pour annuler la fermeture.

Vous pouvez soit accrocher cet événement dans code-behind et exécuter la commande manuellement, ou, et ce serait l'approche préférée, vous pouvez utiliser un comportement ci-joint pour accrocher l'événement et déclencher la commande.

Quelque chose le long des lignes de (et je ne l'ai pas testé):

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.Windows.Interactivity; 
using System.Windows.Input; 

namespace Behaviors 
{ 
    public class WindowCloseBehavior : Behavior<Window> 
    { 
     /// <summary> 
     /// Command to be executed 
     /// </summary> 
     public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(WindowCloseBehavior), new UIPropertyMetadata(null)); 

     /// <summary> 
     /// Gets or sets the command 
     /// </summary> 
     public ICommand Command 
     { 
      get 
      { 
       return (ICommand)this.GetValue(CommandProperty); 
      } 

      set 
      { 
       this.SetValue(CommandProperty, value); 
      } 
     } 

     protected override void OnAttached() 
     { 
      base.OnAttached(); 

      this.AssociatedObject.Closing += OnWindowClosing; 
     } 

     void OnWindowClosing(object sender, System.ComponentModel.CancelEventArgs e) 
     { 
      if (this.Command == null) 
       return; 

      // Depending on how you want to work it (and whether you want to show confirmation dialogs etc) you may want to just do: 
      // e.Cancel = !this.Command.CanExecute(); 
      // This will cancel the window close if the command's CanExecute returns false. 
      // 
      // Alternatively you can check it can be excuted, and let the command execution itself 
      // change e.Cancel 

      if (!this.Command.CanExecute(e)) 
       return; 

      this.Command.Execute(e); 
     } 

     protected override void OnDetaching() 
     { 
      base.OnDetaching(); 

      this.AssociatedObject.Closing -= OnWindowClosing; 
     } 

    } 
}