gpt4 book ai didi

wpf - DialogResult WPF

转载 作者:行者123 更新时间:2023-12-04 13:21:07 26 4
gpt4 key购买 nike

我正在读一本书,上面写着

Rather than setting the DialogResult by hand after the user clicks a button, you can designate a button as the accept button (by setting IsDefault to true). Clicking that button automatically sets the DialogResult of the window to true. Similarly, you can designate a button as the cancel button (by setting IsCancel to true), in which case clicking it will set the DialogResult to 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。

最佳答案

IsDefault ties the button to the Enter key, so that pressing the Enter key will fire the Click event. It does not mean that the Yes button will return true for the DialogResult.



请参阅链接。它将为您清除所有内容

http://blog.wpfwonderland.com/2010/03/22/getting-a-dialogresult-from-a-wpf-window/

http://www.wpftutorial.net/Dialogs.html

希望能帮助到你...

关于wpf - DialogResult WPF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4536988/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com