如何创建一个对话框来提示用户在 WPF 中选择是/否选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6452139/
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
How to Create a DialogBox to prompt the user for Yes/No option in WPF
提问by Shamim Hafiz
I know how to do this on Windows Form App, but I couldn't find anyway of doing so on a WPF App. How would I present the user a blocking DialogBox with Yes/No option and get/process the response from the user?
我知道如何在 Windows 窗体应用程序上执行此操作,但无论如何我都找不到在 WPF 应用程序上执行此操作的方法。我将如何向用户显示一个带有 Yes/No 选项的阻塞 DialogBox 并获取/处理来自用户的响应?
回答by rid
Here's 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;
}
回答by Frinavale
Please note that while Radu's answer works, you cannot apply WPF styles to the MessageBox.
请注意,虽然 Radu 的答案有效,但您不能将 WPF 样式应用于 MessageBox。
I took a different approach to this problem.
我对这个问题采取了不同的方法。
I created a class to serve as a View Model for my message window and I created a style for how I wanted my window to appear. Later in code I instantiated a new Window, set it's DataContext to an instance of my View Model, and set the Window's Style property to the style I created for the window.
我创建了一个类作为我的消息窗口的视图模型,并创建了一个样式来显示我希望我的窗口的显示方式。稍后在代码中,我实例化了一个新窗口,将它的 DataContext 设置为我的视图模型的一个实例,并将 Window 的 Style 属性设置为我为该窗口创建的样式。
I know it sounds a bit overkill, and I'm not sure how other people go about solving this same issue... but my solution is quite flexible and I'm starting to really like it.
我知道这听起来有点矫枉过正,我不确定其他人如何解决同样的问题……但我的解决方案非常灵活,我开始非常喜欢它。
For example, here is 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
Here is my style for a basic "message" window:
这是我的基本“消息”窗口样式:
<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>
My CustomDialogWindow is simply a window with nothing in it:
我的 CustomDialogWindow 只是一个没有任何内容的窗口:
(XAML)
(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>
And in the CustomDialogWindow I have the following code so that the window closes when the user clicks cancel or ok:
在 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
Now when I need to use the window I just instantiate a new CustomDialogWindow, set it's DataContext to a new instance of the DialogViewModel class, and set it's style to the "myMessageStyle":
现在,当我需要使用该窗口时,我只需实例化一个新的 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
The reason why I like this approach is because I inherit from the MyDialogViewModel and provide more properties so that, for instance, I can display a bunch of options for the user to choose from. I just supply custom styles for each type of window I want to display (making sure to bind the appropriate properties). Like I said, it's very flexible and pretty simple to implement.
我喜欢这种方法的原因是因为我从 MyDialogViewModel 继承并提供了更多属性,例如,我可以显示一堆选项供用户选择。我只是为我想要显示的每种类型的窗口提供自定义样式(确保绑定适当的属性)。就像我说的,它非常灵活且易于实现。
Cheers!
干杯!
-Frinny
-弗林尼