2011-07-30 3 views
1

이것은 C# WPF 응용 프로그램입니다. 응용 프로그램을 닫는 Button 있습니다. Window의 오른쪽 위 모서리에있는 (x) 버튼의 기본 닫기 이벤트가 있습니까? [- 답변 편집]XAML의 기본 닫기 이벤트는 무엇입니까?

내가 무엇을 찾고 있었다

<Button Click="CloseWindow_Click"></Button> 

private void Close(object sender, RoutedEventArgs e) 
{ 
    Window.Close(); 
} 

이의 대신

<Button CloseWhenClick></Button> 

:

는 기본적으로 나는 다음과 같은 순수 XAML 솔루션이 희망 대상 :

<Button Command="Close"></Button> 
+0

'Pure XAML'솔루션에 대한 구체적인 이유는 무엇입니까? –

답변

1

"기본"닫는 이벤트와 같은 그런 개념입니다이 없습니다 시도하지만, THRE는 WPF의 명령 개념이다. 해당 버튼에 릴레이 명령을 바인드하면됩니다. 다음은 Q & A : WPF Standard Commands - Where's Exit?에 대한 링크입니다.

희망이 도움이됩니다.

감사합니다.

0

public App() 
    { 
     Exit += new ExitEventHandler(App_Exit); 
    } 

    void App_Exit(object sender, ExitEventArgs e) 
    { 
     // TODO : are you sure ?!! 
    } 

희망이 도움

+0

@ S.Aamani : 언급 한 질문과 같이 XAML 솔루션의 가능성을 찾고 있습니다 – KMC

+0

아니요 불가능합니다. – saber

0

실제로 복잡하지는 않습니다 (하지만 아직도 제공하지 않으면 M $입니다). 여기에 가서 :

public static class MyCommands 
{ 
    private static readonly ICommand appCloseCmd = new ApplicationCloseCommand(); 
    public static ICommand ApplicationCloseCommand 
    { 
     get { return appCloseCmd; } 
    } 
} 

//=================================================================================================== 
public class ApplicationCloseCommand : ICommand 
{ 
    public event EventHandler CanExecuteChanged 
    { 
     // You may not need a body here at all... 
     add { CommandManager.RequerySuggested += value; } 
     remove { CommandManager.RequerySuggested -= value; } 
    } 

    public bool CanExecute(object parameter) 
    { 
     return Application.Current != null && Application.Current.MainWindow != null; 
    } 

    public void Execute(object parameter) 
    { 
     Application.Current.MainWindow.Close(); 
    } 
} 

그리고 AplicationCloseCommand.CanExecuteChanged 이벤트 처리기의 본문이 필요하지 않을 수도 있습니다.

당신은 너무처럼 사용

<MenuItem Header="{DynamicResource MenuFileExit}" Command="MyNamespace:MyCommands.ApplicationCloseCommand"/> 

건배!

(이 명령을 직접 발견하는 데 걸리는 시간을 상상할 수는 없습니다 ...)

관련 문제