سؤال

الفكرة الأساسية وراء إلغاء الزر هو تمكين إغلاق النافذة الخاصة بك مع هروب Keypress.

يمكنك تعيين IsCancel على الممتلكات إلغاء زر صحيح ، مما تسبب في إلغاء زر إغلاق تلقائيا الحوار دون التعامل مع النقر هذا الحدث.

المصدر:برمجة WPF (جريفيث تبيع)

لذلك هذا يجب أن تعمل

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

ومع ذلك السلوك أتوقع لا يعمل بالنسبة لي.الإطار الأصل هو نافذة التطبيق الرئيسية المحددة من قبل التطبيق.StartupUri الملكية.ماذا يعمل

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

private void CloseWindow(object sender, RoutedEventArgs) 
{
    this.Close();
}
  • هو سلوك IsCancel مختلفة استنادا إلى ما إذا كان الإطار هو إطار عادي أو الحوار ؟ لا IsCancel العمل المعلن عنها فقط إذا ShowDialog وقد دعا ؟
  • هو صريح انقر فوق معالج المطلوبة زر (مع IsCancel تعيين إلى true) لإغلاق نافذة على الهروب من الصحافة ؟
هل كانت مفيدة؟

المحلول

نعم, أنها لا تعمل إلا في الحوارات العادية نافذة لا يوجد لديه مفهوم "إلغاء" إنه نفس DialogResult.إلغاء عودته من ShowDialog في WinForms.

إذا أردت إغلاق نافذة مع الهروب يمكنك إضافة معالج PreviewKeyDown على نافذة صغيرة على ما إذا كان هو المفتاح.الهروب و إغلاق النموذج:

public MainWindow()
{
    InitializeComponent();

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

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

نصائح أخرى

نحن يمكن أن تأخذ ستيف الإجابة خطوة واحدة أبعد من ذلك إنشاء مرفق أن "الهروب على وثيقة" وظيفة في أي إطار.كتابة الخاصية مرة واحدة واستخدامها في أي نافذة.فقط إضافة التالية إلى إطار 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 يقول هذا:عند تعيين IsCancel خاصية زر صحيح ، يمكنك إنشاء زر مسجلة مع AccessKeyManager.الزر ثم يتم تفعيلها عندما يضغط المستخدم على مفتاح ESC.لذلك تحتاج إلى معالج في التعليمات البرمجية خلف و أنت لا تحتاج إلى أي تعلق خصائص أو شيء من هذا القبيل

نعم هذا هو الحق.في نظام التشغيل windows تطبيق WPF 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