简体   繁体   English

使用ContextMenu中的CommandParameter传递窗口

[英]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. 目标是在MVVM解决方案中使用硬编码WPF NotifyTrayIcon隐藏/显示任务托盘中的窗口。 The problem is the CommandParameter always seems to be null, which then of course cause the code to crash. 问题是CommandParameter似乎总是为null,这当然会导致代码崩溃。 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? 等等,但一切都传回null,任何建议?

Thanks 谢谢

If you have only one Window then you could simply use Application.Current.MainWindow . 如果你只有一个Window,那么你可以简单地使用Application.Current.MainWindow

If you have more then you may search for the good one in the Application.Current.Windows collection. 如果您有更多,那么您可以在Application.Current.Windows集合中搜索好的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM