문제

취소 버튼의 기본 아이디어는 탈출 키 프레스로 창을 닫을 수 있도록하는 것입니다.

취소 버튼에서 iScancel 속성을 true로 설정하여 CANCEL 버튼이 클릭 이벤트를 처리하지 않고 대화 상자를 자동으로 닫을 수 있습니다.

출처 : 프로그래밍 WPF (Griffith, 판매)

그래서 이것은 작동해야합니다

<Window>
<Button Name="btnCancel" IsCancel="True">_Close</Button>
</Window>

그러나 내가 기대하는 행동은 저에게 효과적이지 않습니다. 상위 창은 application.startupuri 속성에 의해 지정된 기본 응용 프로그램 창입니다. 작동하는 것은

<Button Name="btnCancel" IsCancel=True" Click="CloseWindow">_Close</Button>

private void CloseWindow(object sender, RoutedEventArgs) 
{
    this.Close();
}
  • 창이 일반 창인지 대화 상자인지에 따라 iScancel의 동작이 다릅니 까? IsCancel은 ShowDialog가 호출 된 경우에만 광고 된대로 작동합니까?
  • Escape Press의 창을 닫기 위해 버튼 (Iscancel을 true로 설정 한 상태)에 명시적인 클릭 핸들러가 필요합니까?
도움이 되었습니까?

해결책

예, 일반 창은 "취소"라는 개념이 없기 때문에 대화에서만 작동합니다. Dialogresult.cancel은 Winforms의 ShowDialog에서 돌아 오는 것과 동일합니다.

탈출로 창을 닫으려면 창에서 미리키 다운에 핸들러를 추가 할 수 있습니다. 키인지 여부를 픽업하고 양식을 닫으십시오.

public MainWindow()
{
    InitializeComponent();

    this.PreviewKeyDown += new KeyEventHandler(CloseOnEscape);
}

private void CloseOnEscape(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Escape)
        Close();
}

다른 팁

Steve의 답변을 한 단계 더 발전시키고 모든 창에 대한 "가까운 닫기"기능을 제공하는 첨부 된 속성을 만들 수 있습니다. 속성을 한 번 작성하고 어떤 창에 사용하십시오. Window XAML에 다음을 추가하십시오.

yournamespace:WindowService.EscapeClosesWindow="True"

부동산 코드는 다음과 같습니다.

using System.Windows;
using System.Windows.Input;

/// <summary>
/// Attached behavior that keeps the window on the screen
/// </summary>
public static class WindowService
{
   /// <summary>
   /// KeepOnScreen Attached Dependency Property
   /// </summary>
   public static readonly DependencyProperty EscapeClosesWindowProperty = DependencyProperty.RegisterAttached(
      "EscapeClosesWindow",
      typeof(bool),
      typeof(WindowService),
      new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnEscapeClosesWindowChanged)));

   /// <summary>
   /// Gets the EscapeClosesWindow property.  This dependency property 
   /// indicates whether or not the escape key closes the window.
   /// </summary>
   /// <param name="d"><see cref="DependencyObject"/> to get the property from</param>
   /// <returns>The value of the EscapeClosesWindow property</returns>
   public static bool GetEscapeClosesWindow(DependencyObject d)
   {
      return (bool)d.GetValue(EscapeClosesWindowProperty);
   }

   /// <summary>
   /// Sets the EscapeClosesWindow property.  This dependency property 
   /// indicates whether or not the escape key closes the window.
   /// </summary>
   /// <param name="d"><see cref="DependencyObject"/> to set the property on</param>
   /// <param name="value">value of the property</param>
   public static void SetEscapeClosesWindow(DependencyObject d, bool value)
   {
      d.SetValue(EscapeClosesWindowProperty, value);
   }

   /// <summary>
   /// Handles changes to the EscapeClosesWindow property.
   /// </summary>
   /// <param name="d"><see cref="DependencyObject"/> that fired the event</param>
   /// <param name="e">A <see cref="DependencyPropertyChangedEventArgs"/> that contains the event data.</param>
   private static void OnEscapeClosesWindowChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
   {
      Window target = (Window)d;
      if (target != null)
      {
         target.PreviewKeyDown += new System.Windows.Input.KeyEventHandler(Window_PreviewKeyDown);
      }
   }

   /// <summary>
   /// Handle the PreviewKeyDown event on the window
   /// </summary>
   /// <param name="sender">The source of the event.</param>
   /// <param name="e">A <see cref="KeyEventArgs"/> that contains the event data.</param>
   private static void Window_PreviewKeyDown(object sender, KeyEventArgs e)
   {
      Window target = (Window)sender;

      // If this is the escape key, close the window
      if (e.Key == Key.Escape)
         target.Close();
   }
}

MSDN은 다음과 같이 말합니다. 그런 다음 사용자가 ESC 키를 누르면 버튼이 활성화됩니다. 따라서 코드 뒤에 핸들러가 필요하며 첨부 된 속성이나 그와 비슷한 것이 필요하지 않습니다.

예, WPF의 Windows 응용 프로그램에서 acceptbutton 및 취소 버튼이 있습니다. 그러나 한 가지는 제어 가시성을 허위로 설정하는 경우 WPF에서 가시성만큼 가시성을 만들어야하기 때문에 예상대로 작동하지 않는다는 것입니다. 예 : (여기서 가시성이 거짓이기 때문에 취소 버튼에서 작동하지 않습니다)

<Button x:Name="btnClose" Content="Close" IsCancel="True" Click="btnClose_Click" Visibility="Hidden"></Button> 

그래서 당신은 그것을 만들 필요가 있습니다 :

<Button x:Name="btnClose" Content="Close" IsCancel="True" Click="btnClose_Click"></Button>

그런 다음 글이 있습니다 btnClose_Click 파일 뒤에 코드 :

private void btnClose_Click (object sender, RoutedEventArgs e)
    { this.Close(); }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top