如何在WPF中创建一个对话框以提示用户选择是/否选项

16

我知道如何在Windows Form App上实现这个功能,但是我找不到在WPF App上实现的方法。我应该如何展示一个带有Yes/No选项的阻塞式对话框,并且获取/处理用户的响应?

2个回答

36

这是一个例子:

string sMessageBoxText = "Do you want to continue?";
string sCaption = "My Test Application";

MessageBoxButton btnMessageBox = MessageBoxButton.YesNoCancel;
MessageBoxImage icnMessageBox = MessageBoxImage.Warning;

MessageBoxResult rsltMessageBox = MessageBox.Show(sMessageBoxText, sCaption, btnMessageBox, icnMessageBox);

switch (rsltMessageBox)
{
    case MessageBoxResult.Yes:
    /* ... */
    break;

    case MessageBoxResult.No:
    /* ... */
    break;

    case MessageBoxResult.Cancel:
    /* ... */
    break;
}

那么它和 Windows Form 应用程序是一样的吗? - Shamim Hafiz - MSFT
你需要使用 System.Windows.MessageBox这里是 MessageBox.Show 的文档,它是 PresentationFramework 组件的一部分。 - rid

4
请注意,虽然Radu的答案是可行的,但你不能将WPF样式应用于MessageBox。
我采取了另一种方法来解决这个问题。
我创建了一个类作为我的消息窗口的视图模型,并为我想要窗口出现的方式创建了一个样式。稍后在代码中,我实例化了一个新的Window,将其DataContext设置为我的View Model的实例,并将Window的Style属性设置为我为Window创建的样式。
我知道这听起来有点过度,而且我不确定其他人如何解决同样的问题...但我的解决方案非常灵活,我开始真的很喜欢它。
例如,这是Dialog View Model:
Public Class MyDialogViewModel
  Public Event Closed()

  Public Property Message As String

  Public Property Cancel As MyNamespace.RelayCommand
  Public Property Close As MyNamespace.RelayCommand
  Public Property WasCancelled As Boolean

  Public Sub New()
    WasCancelled = True
    Cancel = New MyNamespace.RelayCommand(AddressOf CancelClicked)
    Close = New MyNamespace.RelayCommand(AddressOf CloseClicked)
  End Sub

  Private Sub CancelClicked()
    RaiseEvent Closed()
  End Sub
  Private Sub CloseClicked()
    WasCancelled = False
    RaiseEvent Closed()
  End Sub
End Class

这是我为基本的“消息”窗口设计的样式:

    <Style x:Key="myMessageStyle" TargetType="{x:Type myNameSpace:CustomDialogWindow}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <ControlTemplate.Resources>
                        <Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
                            <Setter Property="Width" Value="100"/>
                            <Setter Property="Height" Value="25"/>
                        </Style>
                    </ControlTemplate.Resources>
                    <Border >
                        <DockPanel Margin="10,10,0,10">
                            <TextBlock Text="{Binding Message}" Width="Auto" TextWrapping="WrapWithOverflow" DockPanel.Dock="Top" 
                                       Margin="10"
                                       Foreground="{StaticResource MyMessageBoxForegroundColor}"/>
                            <DockPanel Margin="5,0,0,0" DockPanel.Dock="Bottom">
                                <Button Content="Ok" Command="{Binding Close}" ></Button>
                                <Button Content="Cancel" Command="{Binding Cancel}"  HorizontalAlignment="Stretch"></Button>
                            </DockPanel>
                        </DockPanel>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

我的CustomDialogWindow只是一个没有任何内容的窗口:

(XAML)

<Window x:Class="CustomDialogWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="CustomDialogWindow"
        SizeToContent="WidthAndHeight">

</Window>

在CustomDialogWindow中,我有以下代码,以便在用户单击取消或确定时关闭窗口:
Public Class CustomDialogWindow 

    Private Sub CustomDialogWindow_DataContextChanged(ByVal sender As Object, ByVal e As System.Windows.DependencyPropertyChangedEventArgs) Handles Me.DataContextChanged
        Dim dContext As MyDialogViewModel= TryCast(DataContext, MyDialogViewModel)
        If dContext IsNot Nothing Then
            AddHandler DirectCast(DataContext, MyDialogViewModel).CloseWindow, AddressOf CloseWindow
        End If
    End Sub
    Private Sub CloseWindow()
        Me.Close()
    End Sub
End Class

现在当我需要使用窗口时,我只需实例化一个新的CustomDialogWindow,将其DataContext设置为DialogViewModel类的一个新实例,并将其样式设置为"myMessageStyle":

Dim cdw As New CustomDialogWindow
Dim dvm As New DialogViewModel
dvm.Message = "Hello World!"
cdw.DataContext = dvm
cdw.ShowDialog()

If dvm.WasCancelled = False Then 
  '....'
End If

我喜欢这种方法的原因是因为我继承了MyDialogViewModel并提供更多属性,以便例如我可以为用户展示一堆选项供其选择。我只需要为我想要显示的每种类型的窗口提供自定义样式(确保绑定相应的属性)。像我说的那样,它非常灵活且实现起来很简单。
干杯!
-Frinny

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