MVVM, DialogService and Dialog Result

与世无争的帅哥 提交于 2019-12-14 03:53:05

问题


I'm currently learning WPF/MVVM, and have been using the code in the following question to display dialogs using a Dialog Service (including the boolean change from Julian Dominguez):

Good or bad practice for Dialogs in wpf with MVVM?

Displaying a dialog works well, but the dialog result is always false despite the fact that the dialog is actually being shown. My DialogViewModel is currently empty, and I think that maybe I need to "hook up" my DialogViewModel to the RequestCloseDialog event. Is this the case?


回答1:


does your DialogViewmodel implement IDialogResultVMHelper? and does your View/DataTemplate has a Command Binding to your DialogViewmodel which raise the RequestCloseDialog?

eg

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

anywhere in your Dialog control:

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

and then your result should work in your viewmodel

var dialog = new DialogViewmodel();
var result = _dialogservice.ShowDialog("My Dialog", dialog );

if(result.HasValue && result.Value)
{
    //accept true
}
else
{
    //Cancel or false
}


来源:https://stackoverflow.com/questions/25779674/mvvm-dialogservice-and-dialog-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!