내 WPF 유지 관리 창에는 "종료"버튼이있는 툴바가 있습니다. CommandExit는이 단추와 바인딩됩니다. CommandExit은 종료 전에 일부 검사를 수행합니다.윈도우 제목 표시 줄의 X 버튼에 명령 바인딩
이제 창 (제목 표시 줄의 x 버튼)을 닫으면이 검사가 무시됩니다.
CommandExit을 창 x 단추에 바인드하려면 어떻게해야합니까?
내 WPF 유지 관리 창에는 "종료"버튼이있는 툴바가 있습니다. CommandExit는이 단추와 바인딩됩니다. CommandExit은 종료 전에 일부 검사를 수행합니다.윈도우 제목 표시 줄의 X 버튼에 명령 바인딩
이제 창 (제목 표시 줄의 x 버튼)을 닫으면이 검사가 무시됩니다.
CommandExit을 창 x 단추에 바인드하려면 어떻게해야합니까?
당신은 당신이 검사를하고 닫는 작업을 취소 할 수 있습니다 메인 윈도우의 이벤트 "닫기"의 이벤트 핸들러를 구현해야 . 이것은 가장 쉬운 방법이지만 그렇지 않으면 전체 창과 테마를 다시 디자인해야합니다.
이러한 조건을 기반으로 결산을 취소 하시겠습니까? Closing event을 사용하면 System.ComponentModel.CancelEventArgs를 전달하여 닫기를 취소 할 수 있습니다.
코드 숨김으로이 이벤트를 연결하고 수동으로 명령을 실행하거나 선호하는 방법 일 수 있습니다. 연결된 동작을 사용하여 이벤트를 연결하고 명령을 실행할 수 있습니다. 의 라인을 따라
뭔가 (내가이 테스트를하지 않은 경우) :
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;
}
}
}