C# MVVM、对话服务和对话结果

C# MVVM、对话服务和对话结果,c#,wpf,mvvm,dialog,modal-dialog,C#,Wpf,Mvvm,Dialog,Modal Dialog,我目前正在学习WPF/MVVM,并一直在使用以下问题中的代码使用对话服务显示对话框(包括Julian Dominguez的布尔更改): 显示对话框效果很好,但是对话框结果总是错误的,尽管对话框实际上正在显示。我的DialogViewModel当前为空,我认为可能需要将DialogViewModel“连接”到RequestCloseDialog事件。是这样吗?您的DialogViewmodel是否实现IDialogResultVMHelper?视图/数据模板是否有命令绑定到引发RequestCl

我目前正在学习WPF/MVVM,并一直在使用以下问题中的代码使用对话服务显示对话框(包括Julian Dominguez的布尔更改):


显示对话框效果很好,但是对话框结果总是错误的,尽管对话框实际上正在显示。我的DialogViewModel当前为空,我认为可能需要将DialogViewModel“连接”到RequestCloseDialog事件。是这样吗?

您的DialogViewmodel是否实现IDialogResultVMHelper?视图/数据模板是否有命令绑定到引发RequestCloseDialog的DialogViewmodel

乙二醇


谢谢真是太棒了!另外,我还了解了懒惰加载的世界!
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);
    }
}
<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
}