0

I stumbled upon the problem of not being able to use a windows method as a property inside the objects initializer:

  var window = new DialogWindow
  {
    DataContext = new YesNoDialogViewModel()
    {
      DialogMessage = confirmation.Content as string,
      DialogTitle = confirmation.Title,
    }
  };

  (window.DataContext as YesNoDialogViewModel).CloseWindowCommand = new ActionCommand(window.Close);

I wan't to do something like this:

var window = new DialogWindow
  {
    DataContext = new YesNoDialogViewModel()
    {
      DialogMessage = confirmation.Content as string,
      DialogTitle = confirmation.Title,
      CloseWindowCommand = new ActionCommand(window.Close)
    }
  };

However, i can't seem to figure it out (new ActionCommand(window.Close) doesnt compile, cannot use window before its declared).

Is there any hack or workaround to just add the reference to the void anyways?

Mafii
  • 7,227
  • 1
  • 35
  • 55

1 Answers1

2

You can't put it in object initializer because ActionCommand() uses in argument the object which is not created yet - window -> window.Close().

Roman
  • 11,966
  • 10
  • 38
  • 47