DialogResult WPF

6
我正在阅读一本书,其中提到:
与其在用户单击按钮后手动设置 DialogResult,您可以将某个按钮指定为“接受”按钮(通过将 IsDefault 设置为 true)。单击该按钮会自动将窗口的 DialogResult 设置为 true。同样,您可以将某个按钮指定为“取消”按钮(通过将 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>   

这是相关的IT技术代码:

这是代码:

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

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

无论我点击默认按钮还是取消按钮,它都只返回false的DialogResult。

4个回答

6

将你的代码修改为

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

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

希望这能帮到您。

嗨,你没有读作者的话。他说DialogResult会自动设置。 - Akshay J
我刚刚测试了一下,对话框结果没有自动设置为任何一个按钮。然而,奇怪的是,设置为IsDefault的按钮在单击时不会自动关闭窗口。然而,设置为IsCancel的按钮却可以。还有其他人看到这种奇怪的行为吗? - Mike Loux
1
我应该稍微澄清一下。如果你没有指定任何处理程序(或者在任何处理程序中都没有指定DialogResult的值),那么设置了IsCancel属性的按钮仍然会关闭窗口。而设置了IsDefault属性的按钮则不会。但是,如果你在两个事件处理程序中都设置了DialogResult,那么窗口将会关闭(甚至不需要使用this.Close()这一行代码)。非常奇怪而且非常不一致。 - Mike Loux
是的,Mike Loux,我完全同意你的看法。这非常不一致。 - Akshay J
7
在.NET 4.5中,您不需要调用this.Close()。将Window.DialogResult属性设置为true或false会自动关闭对话框/窗口,无需额外操作。 - Paul-Sebastian Manole

6

2
第一个链接坏了 :( - juagicre

2
据我了解,将IsDefault设置为true并将IsCancel设置为false只能使您分配事件应发生的内容,即对于IsCancel,窗口将在“Escape”键上触发关闭事件,而对于IsDefault = true,则为Enter键。

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


1
使用Net 5,这似乎是打开ShowDialog窗口并关闭它所需的全部代码。从您已经打开的窗口中。
   <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;
    }

从函数到打开窗口。
        var requester = new DeleteRequester();// a wpf window       
        var showDialog = requester.ShowDialog( );
        if (showDialog != null && showDialog.Value)

唯一检查是否为空的原因是为了消除ReSharper中的蓝色警告线。
似乎每当更改“DialogResult”时,窗口都会关闭并返回该值。这有点合理,如果您还没有完成,为什么要更改该值呢。
对于您在窗口中进行的任何操作,您只需关闭窗口以返回false结果,或将DialogResult设置为true以使用true结果关闭窗口。
简单明了。
 If(ItWorked){DialogResult = true;}// closes window returns true

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接