【问题标题】:MVVM, DialogService and Dialog ResultMVVM、DialogService 和 Dialog 结果
【发布时间】:2014-11-04 22:39:05
【问题描述】:
【问题讨论】:
标签:
c#
wpf
mvvm
dialog
modal-dialog
【解决方案1】:
您的 DialogViewmodel 是否实现了 IDialogResultVMHelper?并且您的 View/DataTemplate 是否具有与您的 DialogViewmodel 的命令绑定,从而引发 RequestCloseDialog?
例如
public class DialogViewmodel : INPCBase, IDialogResultVMHelper
{
private readonly Lazy<DelegateCommand> _acceptCommand;
public DialogViewmodel()
{
this._acceptCommand = new Lazy<DelegateCommand>(() => new DelegateCommand(() => InvokeRequestCloseDialog(new RequestCloseDialogEventArgs(true)), () => **Your Condition goes here**));
}
public event EventHandler<RequestCloseDialogEventArgs> RequestCloseDialog;
private void InvokeRequestCloseDialog(RequestCloseDialogEventArgs e)
{
var handler = RequestCloseDialog;
if (handler != null)
handler(this, e);
}
}
Dialog 控件中的任何位置:
<StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Right" MinHeight="30">
<Button IsDefault="True" Content="Übernehmen" MinWidth="100" Command="{Binding AcceptCommand}"/>
<Button IsCancel="True" Content="Abbrechen" MinWidth="100"/>
</StackPanel>
然后你的结果应该在你的视图模型中工作
var dialog = new DialogViewmodel();
var result = _dialogservice.ShowDialog("My Dialog", dialog );
if(result.HasValue && result.Value)
{
//accept true
}
else
{
//Cancel or false
}