使用ContextMenu中的CommandParameter传递窗口

本文关键字:窗口 CommandParameter ContextMenu 中的 使用 | 更新日期: 2023-09-27 18:19:56

目标是在MVVM解决方案中使用硬编码WPF NotifyTrayIcon从任务托盘中隐藏/显示窗口。问题是CommandParameter似乎总是为null,这当然会导致代码崩溃。我尝试了许多不同的绑定,包括:

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

等等,但一切都是空的,有什么建议吗?

感谢

使用ContextMenu中的CommandParameter传递窗口

如果您只有一个Window,那么您可以简单地使用Application.Current.MainWindow

如果你有更多,那么你可以在Application.Current.Windows系列中搜索好的。