[英]How does the WPF Button.IsCancel property work?
“取消”按鈕背后的基本思想是使用Escape Keypress關閉窗口。
您可以將“取消”按鈕上的IsCancel屬性設置為true,從而使“取消”按鈕自動關閉對話框而不處理Click事件。
資料來源:編程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();
}
是的,它只適用於對話框,因為普通窗口沒有“取消”的概念,它與從WinForms中的ShowDialog返回的DialogResult.Cancel相同。
如果你想關閉一個帶有轉義的Window,你可以在窗口上為PreviewKeyDown添加一個處理程序,拾取它是否是Key.Escape並關閉表單:
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屬性設置為true時,您將創建一個使用AccessKeyManager注冊的Button。 然后,當用戶按下ESC鍵時,該按鈕被激活。 因此,您需要在代碼中使用處理程序而且您不需要任何附加屬性或類似的東西
是的,這是正確的。在WPF中的Windows應用程序中,AcceptButton和Cancel Button就在那里。 但有一點是,如果您將控件可見性設置為false,則它將無法按預期工作。為此,您需要在WPF中將可見性視為true。 例如:(它不適用於取消按鈕,因為此處可見性為假)
<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 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.