Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个放置在 WPF 窗口内的自定义控件,是否有可能在自定义控件中获取该 WPF 窗口并在该窗口上挂钩一些事件?不使用应用程序类(例如 Application.Current.Mainwindow)
啊...方法怎么样Window.GetWindow?:
Window.GetWindow
private void UserControl_Loaded(object sender, RoutedEventArgs e) { Window parentWindow = Window.GetWindow(this); }
请注意,它在构造函数中不起作用,但是如果您使用Loaded事件,它就可以正常工作。
Loaded