2009-01-07 38 views
31

「取消」按鈕背後的基本思想是通過Escape Keypress啓用關閉窗口。WPF Button.IsCancel屬性如何工作?

可以取消按鈕設置IsCancel屬性上 爲true,導致 取消按鈕自動關閉 對話框不處理點擊 事件。

來源:編程WPF(格里菲斯,銷售)

所以這應該工作

<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不同的基於窗口是否是一個正常的窗口或對話框中的行爲嗎?只有在ShowDialog被調用後,IsCancel才能像廣告一樣工作嗎?
  • 該按鈕是否需要一個明確的Click處理程序(IsCancel設置爲true)來關閉Escape按鈕上的窗口?

回答

29

是的,它只適用於對話框,因爲普通窗口沒有「取消」的概念,它與WinForms中從ShowDialog返回的DialogResult.Cancel相同。

如果你想關閉一個窗口逃生,你可以在它是否是一個Key.Escape處理程序添加到PreviewKeyDown在窗口上,皮卡和關閉形式:

public MainWindow() 
{ 
    InitializeComponent(); 

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

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

我們可以採取史蒂夫的一個答案進一步創建一個附加屬性,爲任何窗口提供「關閉換碼」功能。寫一次財產並在任何窗口中使用它。只需添加下面的窗口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(); 
    } 
} 
+3

是的。附加屬性在我的腦海中仍然不會「點擊」。 – Gishu 2009-02-05 08:35:33

+0

工程很好,唯一的是我必須替換鑄造到窗口,並添加一個檢查null爲空,如此處所示 - http://stackoverflow.com/questions/10206742/unable-to-cast-object-of-type -microsoft-expression-platform-wpf-instancebuilder,以避免錯誤「無法將類型爲」Microsoft.Expression.Platform.WPF.InstanceBuilders.WindowInstance「的對象轉換爲鍵入」System.Windows.Window「」(項目能夠構建成功,但錯誤很煩人,這是爲了我的VS 2012 + R#,並且在此之後,我必須重新啓動VS. – sarh 2015-02-12 22:21:39

6

這是不完全正確是... MSDN這樣說:當您設置將按鈕的IsCancel屬性設置爲true,您將創建一個在AccessKeyManager中註冊的Button。當用戶按ESC鍵時,該按鈕被激活。 所以你需要在後面 代碼中的處理程序,並就不需要任何附加屬性或類似的東西在WPF的AcceptButton是

1

是的,這是right.In Windows應用程序和取消按鈕是存在的。但有一點是,如果您將控件可見性設置爲false,那麼它將無法按預期工作。爲此,您需要在WPF中將可見性設置爲true。例如:(它不工作了取消按鈕,因爲這裏的能見度爲false)

<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(); }