Search code examples
c#wpfmvvmtrayicon

pass the window using a CommandParameter from a ContextMenu


The goal is to hide/show a window from the task tray using Hardcoded WPF NotifyTrayIcon in a MVVM solution. The problem is the CommandParameter always seems to be null, which then of course cause the code to crash. I've tried a number of different bindings including:

CommandParameter="{Binding Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MainWindow}}}"

CommandParameter="{Binding ElementName=window, Mode=OneWay}"

etc but everything passes back a null, any suggestions?

Thanks


Solution

  • If you have only one Window then you could simply use Application.Current.MainWindow.

    If you have more then you may search for the good one in the Application.Current.Windows collection.