I am working on a Revit plugin and I am forced to work through a command. This command has a parameter that I need to change information inside Revit: UIApplication
.
From the command I open a new Window
, this Window
creates it's ViewModel
through XAML:
<Window.DataContext>
<!--Automatically creates an instance of the class-->
<plugin:PluginViewModel />
</Window.DataContext>
I would like to create my ViewModel
with the UIApplication
parameter, which is supplied through the constructor of my Window
. Is this the only way to do it?
public PluginWindow(UIApplication application)
{
InitializeComponent();
//DataContext has been initialized by InitializeComponent();
var pluginVM = (PluginViewModel)DataContext;
pluginVM.Application=application;
}
Or should I do it through the command?
var pw = new PluginWindow();
var pluginVM = (PluginViewModel)pw.DataContext;
pluginVM.Application=application;
Or is it possible to do in the XAML of the Window
?
<Window.DataContext>
<!--Automatically creates an instance of the class-->
<plugin:PluginViewModel SomeUnknownMagicHere/>
</Window.DataContext>
The last one would be preferred.
I could also have the UIApplication
available as a static variable somewhere, but this would be somewhat ugly, right?