Right way here is to not pass parent Window
to the VM as CommandParameter
. If this is MVVM you should be using a Messenger(MVVM Light) / EventAggregator(Prism) approach to send a Message to the Window
's code-behind when the command is triggered to Close it.
Referencing Window
in the VM is just plain wrong.
Just for reference, what your trying to do "can be done"
something like:
<Grid x:Name="LayoutRoot">
<Button HorizontalAlignment="Left"
Margin="0"
Style="{DynamicResource ButtonStyle1}"
VerticalAlignment="Top"
Width="120"
Height="25"
Content="Dashboard Menu"
TextElement.FontWeight="Bold"
Foreground="AliceBlue"
Tag="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type Window}}}">
<Button.ContextMenu>
<ContextMenu>
<MenuItem Header="Open Log Viewer" Command="{StaticResource openLogViewer}" />
<Separator />
<MenuItem Command="{StaticResource exit}"
CommandParameter="{Binding PlacementTarget.Tag,
RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"
Header="Exit" />
...
Update:
When executing the "Exit" command from the ContextMenu
you should see Sender Object: MvvmLight16.MainWindow
in your Output Window. This output is sent from the VM.