C# 取消按钮关闭窗口
谢谢你阅读我的帖子 我正在尝试插入一个取消按钮,它基本上与窗口的X按钮相同 这是来自的代码。我也有同样的情况C# 取消按钮关闭窗口,c#,wpf,C#,Wpf,谢谢你阅读我的帖子 我正在尝试插入一个取消按钮,它基本上与窗口的X按钮相同 这是来自的代码。我也有同样的情况 public partial class Dialog : Window { . . . private void Window_Closing(object sender, CancelEventArgs e) { e.Cancel() = true; //Works as expected } privat
public partial class Dialog : Window
{
.
.
.
private void Window_Closing(object sender, CancelEventArgs e)
{
e.Cancel() = true; //Works as expected
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
e.Cancel() = true; //Compile error
}
}
以下是xaml:
<Window x:Class="ExperimentSettingsViewer.TemplateName"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Template Name"
Height="120"
Width="300"
WindowStartupLocation="CenterOwner">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/ExperimentSettingsViewer;component/Button.xaml" />
<ResourceDictionary Source="/ExperimentSettingsViewer;component/Window.xaml" />
<ResourceDictionary Source="/ExperimentSettingsViewer;component/Border.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<StackPanel>
<TextBox Name="tbName"
Margin="3"
Text="{Binding Path=NewName}" />
<StackPanel Orientation="Horizontal" HorizontalAlignment="right">
<Button Name="btnOK"
Content="OK"
Width="75"
Height="30"
HorizontalAlignment="Right"
Margin="3"
Click="btnOK_Click" />
<Button Name="btnCancel"
Content="Cancel"
Width="75"
Height="30"
HorizontalAlignment="Right"
Margin="3"
Click="btnCancel_Click" IsCancel="True" />
</StackPanel>
</StackPanel>
</Grid>
</Window>
但是,我遵循该线程中的解决方案,将buttom的IsCancel设置为true,但仍然没有可用于我的按钮的Cancel()方法
我想知道我错过了什么吗?或者如何使我的取消按钮像X按钮一样工作。非常感谢。按钮。IsCancel属性不会自动关闭窗口。它所做的只是允许您使用Escape“按下”按钮。您仍然需要在单击事件处理程序中调用
Close()
,以关闭窗口
因此,该事件没有
Cancel
属性。如果不想关闭窗口,只需不要调用close()
按钮即可。IsCancel属性不会自动关闭窗口。它所做的只是允许您使用Escape“按下”按钮。您仍然需要在单击事件处理程序中调用Close()
,以关闭窗口
因此,该事件没有Cancel
属性。如果不想关闭该窗口,请不要调用close()