比较来自世界各地的卖家的域名和 IT 服务价格

如何创建 DialogBox, 要求用户选项是/不 WPF

我知道如何在申请中进行 Windows Form, 但仍然无法找到在申请中进行方法 WPF.
好像我介绍了用户阻塞 DialogBox 有选择 Yes/No 并收到 / 程序来自用户的答案?
已邀请:

涵秋

赞同来自:

这里
http://dotnetbhupesh.com/post/ ... .aspx
:


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

裸奔

赞同来自:

请注意,虽然答案是RADA,但您无法申请 WPF 款式K. MessageBox.

我对这个问题有不同的方法。

我创建了一个类用作我的消息窗口的演示模型,我创建了一种风格,了解我希望我的窗口看起来。 稍后在代码中,我创建了一个新窗口的实例,安装了它 DataContext 在我的演示模型的实例中,并以我为窗口创建的样式安装窗口样式属性。

我知道它听起来有点不必要,我不确定别人如何决定同样的问题......但我的决定是非常灵活的,它开始了我。

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


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


这是我的基础窗口的风格 "message":


<style targettype="{x:Type myNameSpace:CustomDialogWindow}" x:key="myMessageStyle">
<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>

<border>
<dockpanel margin="10,10,0,10">
<textblock dockpanel.dock="Top" foreground="{StaticResource MyMessageBoxForegroundColor}" margin="10" text="{Binding Message}" textwrapping="WrapWithOverflow" width="Auto"></textblock>
<dockpanel dockpanel.dock="Bottom" margin="5,0,0,0">
<button command="{Binding Close}" content="Ok"></button>
<button command="{Binding Cancel}" content="Cancel" horizontalalignment="Stretch"></button>
</dockpanel>
</dockpanel>
</border>





我的customdialogwindow只是一个窗口,没有什么:

/XAML/


<window sizetocontent="WidthAndHeight" title="CustomDialogWindow" x:class="CustomDialogWindow" xmlns="[url=http://schemas.microsoft.com/winfx/2006/xaml/presentation"]http://schemas.microsoft.com/w ... ot%3B[/url] xmlns:x="[url=http://schemas.microsoft.com/winfx/2006/xaml">]http://schemas.microsoft.com/w ... gt%3B[/url]
</window>


A B. 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 而且我提供更多属性,例如,我可以为选择显示一堆用户选项。 我只需为我要显示的每个类型窗口提供自定义样式 /一定要绑定相应的属性/. 正如我所说,实现的实施非常灵活,非常简单。

欢呼!

- 法语

要回复问题请先登录注册