4

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?

Community
  • 1
  • 1
bdan
  • 91
  • 1
  • 5

1 Answers1

2

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
}
Derrick Moeller
  • 4,808
  • 2
  • 22
  • 48
blindmeis
  • 22,175
  • 7
  • 55
  • 74
  • Thanks! That worked great! Plus I got an introduction to the world of Lazy-Loading! – bdan Sep 12 '14 at 00:38