【问题标题】:How to Create a DialogBox to prompt the user for Yes/No option in WPF如何在 WPF 中创建对话框以提示用户输入是/否选项
【发布时间】:2011-06-23 09:46:21
【问题描述】:

我知道如何在 Windows 窗体应用程序上执行此操作,但我无法在 WPF 应用程序上执行此操作。 我如何向用户显示一个带有是/否选项的阻塞对话框并获取/处理用户的响应?

【问题讨论】:

    标签: wpf dialog


    【解决方案1】:

    这里是an example

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

    【讨论】:

    【解决方案2】:

    请注意,虽然 Radu 的回答有效,但您不能将 WPF 样式应用于 MessageBox。

    我对这个问题采取了不同的方法。

    我创建了一个类作为消息窗口的视图模型,并创建了我希望我的窗口出现的样式。后来在代码中我实例化了一个新窗口,将它的 DataContext 设置为我的视图模型的一个实例,并将窗口的 Style 属性设置为我为窗口创建的样式。

    我知道这听起来有点矫枉过正,而且我不确定其他人如何解决同样的问题......但我的解决方案非常灵活,我开始非常喜欢它。

    例如,这里是对话框视图模型:

    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 并提供了更多属性,例如,我可以显示一堆选项供用户选择。我只是为要显示的每种类型的窗口提供自定义样式(确保绑定适当的属性)。就像我说的,它非常灵活,实现起来也很简单。

    干杯!

    -弗林尼

    【讨论】:

      猜你喜欢
      • 2011-02-17
      • 1970-01-01
      • 2012-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-13
      • 2013-12-24
      • 1970-01-01
      相关资源
      最近更新 更多