对话结果 WPF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4536988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:26:46  来源:igfitidea点击:

DialogResult WPF

wpf

提问by Akshay J

I am reading one book which says

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

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.

您可以将一个按钮指定为接受按钮(通过将 IsDefault 设置为 true),而不是在用户单击按钮后手动设置 DialogResult。单击该按钮会自动将窗口的 DialogResult 设置为 true。同样,您可以将一个按钮指定为取消按钮(通过将 IsCancel 设置为 true),在这种情况下,单击它会将 DialogResult 设置为 Cancel。

This is the 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>

Code for click event:

点击事件代码:

private void BtnShowDialogStatus_Click(object sender, RoutedEventArgs e)
{
    MessageBox.Show(new NewWindow().ShowDialog().ToString());
}

And this is the Dialog box which I am opening on the click event:

这是我在点击事件中打开的对话框:

<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>   

This is the code for it:

这是它的代码:

private void BtnDEfault_Click(object sender, RoutedEventArgs e)
{
    this.Close();
}

private void BtnCancel_Click(object sender, RoutedEventArgs e)
{
    this.Close();
}

I can see it returning the DialogResult only as false no matter I click the default or cancel button.

无论我单击默认按钮还是取消按钮,我都可以看到它仅将 DialogResult 返回为 false。

回答by biju

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.

IsDefault 将按钮与 Enter 键联系起来,因此按下 Enter 键将触发 Click 事件。这并不意味着 Yes 按钮将为 DialogResult 返回 true。

Refer to the links.It will clear up things for you

请参阅链接。它会为您清理一切

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

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

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

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

Hope it helps...

希望能帮助到你...

回答by Binil

change your code to

将您的代码更改为

private void BtnDEfault_Click(object sender, RoutedEventArgs e)
{
    DialogResult = true;
    this.Close();
}

private void BtnCancel_Click(object sender, RoutedEventArgs e)
{
    DialogResult = false;
    this.Close();
}

hope this helps

希望这可以帮助

回答by Bhupendra Joshi

To my understanding setting IsDefault as true and IsCancel as false only enables you to assign what event should occur i.e. the window will fire close event on 'Escape' key for IsCancel and for Enter key for IsDefault=true.

根据我的理解,将 IsDefault 设置为 true 并将 IsCancel 设置为 false 只能让您指定应该发生的事件,即窗口将在 IsCancel 的“Escape”键和 IsDefault=true 的 Enter 键上触发关闭事件。

You will need to set the Dialog result from your button click / command action handlers.

您需要从按钮单击/命令操作处理程序设置对话框结果。

回答by Jon

Using net 5 this seems to be all the code needed to open a ShowDialog window, and close it.

使用 net 5 这似乎是打开 ShowDialog 窗口并关闭它所需的所有代码。

From the window you have opened.

从你打开的窗口。

   <Button Margin="10"   IsDefault="True"  Click="Ok_OnClick" >OK</Button>
   <Button  Margin="10" IsCancel="True">Cancel</Button>

    private void Ok_OnClick(object sender, RoutedEventArgs e)
    {
        DialogResult = true;
    }

From function to open window.

从函数到打开窗口。

        var requester = new DeleteRequester();// a wpf window       
        var showDialog = requester.ShowDialog( );
        if (showDialog != null && showDialog.Value)

the only reason it is checked for null is to get rid of the blue nag line from re-sharper.

检查 null 的唯一原因是从 re-sharper 中去除蓝色唠叨线。

It seems whenever you change the "DialogResult" The window is going to close and the value gets returned. Kind of make sense, why would you change the value if you weren't done.

似乎每当您更改“DialogResult”时,窗口就会关闭并返回值。有点道理,如果你没有完成,你为什么要改变价值。

With what ever your doing with the window, you simply need to close the window to return a false result, or set the DialogResult to true to close the window with a true result.

无论您对窗口做什么,您只需关闭窗口即可返回错误结果,或者将 DialogResult 设置为 true 以关闭具有真实结果的窗口。

Simple and basic:

简单和基本:

 If(ItWorked){DialogResult = true;}// closes window returns true

 If(ItsJunk){Close();}// closes window returns false
 If(ItsJunk){DialogResult = false;}//closes window returns false