题
背后的基本思想取消按钮是使闭上你的窗口逃跑按键。
你可以设定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设置为真正的)关闭一个窗口逃生新闻?
解决方案
是的,这只是工作上的对话作为一个正常的窗口的概念"取消",这是同样作为未关闭.取消返回自ShowDialog在它.
如果你想要靠近窗户逃跑你可以添加一个处理程序PreviewKeyDown上的窗口,皮上无论是关键。逃离和接近的形式:
public MainWindow()
{
InitializeComponent();
this.PreviewKeyDown += new KeyEventHandler(CloseOnEscape);
}
private void CloseOnEscape(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape)
Close();
}
其他提示
我们可以采取的史蒂夫回答一个进一步步骤,并创建一个附属性提供了"逃避在附近的"功能的任何窗口。写财产,一旦和利用它在任何窗口。只是添加以下的窗口键
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.例如:(它不是作为取消键因为这里的能见度是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(); }
不隶属于 StackOverflow