我正在读一本书,上面写着
,而不是在用户单击按钮后手动设置DialogResult,您可以指定一个按钮作为接受按钮(通过将IsDefault设置为true)。单击该按钮会自动将窗口的DialogResult设置为true。类似地,您可以指定一个按钮为Cancel按钮(通过将IsCancel设置为true),在这种情况下,单击它将DialogResult设置为cancel。
这是MainWindow:
<Window x:Class="WpfApplicationWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Width="400" Height="400">
<StackPanel>
<Button Name="BtnShowDialogStatus" Click="BtnShowDialogStatus_Click">DIALOG RESULT</Button>
</StackPanel>
</Window>单击事件代码:
private void BtnShowDialogStatus_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(new NewWindow().ShowDialog().ToString());
}这是我在点击事件上打开的对话框:
<Window x:Class="WpfApplicationWPF.NewWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="NewWindow" Height="300" Width="300">
<StackPanel>
<Button Name="BtnDEfault" IsDefault="True" Click="BtnDEfault_Click">DEFAULT BUTTON</Button>
<Button Name="BtnCancel" IsCancel="True" Click="BtnCancel_Click">CANCEL BUTTON</Button>
</StackPanel>
</Window> 这是它的代码:
private void BtnDEfault_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void BtnCancel_Click(object sender, RoutedEventArgs e)
{
this.Close();
}我可以看到它只返回DialogResult为false,无论我点击默认或取消按钮。
发布于 2010-12-27 08:11:46
IsDefault将按钮绑定到Enter键,这样按Enter键就会触发Click事件。这并不意味着“是”按钮将返回DialogResult的true。
参考links.It会帮你清理东西的
http://blog.wpfwonderland.com/2010/03/22/getting-a-dialogresult-from-a-wpf-window/
http://www.wpftutorial.net/Dialogs.html
希望能帮上忙..。
发布于 2010-12-27 07:04:35
将代码更改为
private void BtnDEfault_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
this.Close();
}
private void BtnCancel_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
this.Close();
}希望这能帮上忙
发布于 2010-12-27 08:16:05
据我理解,将IsDefault设置为true,将IsCancel设置为false,只允许您分配应该发生的事件,即窗口将触发IsCancel的'Escape‘键和IsDefault=true的Enter键上的关闭事件。
您需要设置按钮单击/命令操作处理程序的对话框结果。
https://stackoverflow.com/questions/4536988
复制相似问题