处理从主窗口中的模态对话框引发的冒泡事件 [英] Handling a bubbling event raised from a modal dialog in main window
问题描述
我可以在对话框中处理我的事件(在登录successfull时关闭对话框)。但是,我无法在我的主应用程序中处理这个事件(用户登录后我应该刷新UI)。
如何拦截这样的路由事件在窗外被抬起的地方(如果有可能的话)?如果不可能,通常的处理方式是什么?
路由事件不会从新窗口向所有者发送。 RoutedCommands也不会直接工作。但是绑定
可以工作!
当您设置 childWindow.OwnerWindow = Application.Current.MainWindow
childWindow通过它的逻辑连接到MainWindow OwnerWindow
property。
您可以使用它绑定到 OwnerWindow
的ViewModel命令并执行。
< Window x:Class =... ChildWindow
...>
< Button Command ={Binding Path = OwnerWindow.DataContext.SaveCommand,
RelativeSource = {RelativeSource
AncestorType = {x:Type Window}}}
Content =执行所有者的保存命令/>
< / Window>
I have a modal dialog in my WPF application that allows a user to login to a server. The dialog simply contains a User Control that handles all the login stuff (UI, web service call, and raising a routed event when the call returns).
All works nicely and I can handle my event in the dialog (close the dialog when login successfull). However, I am not able to handle the event in my main application (I should refresh the UI once the user has logged in).
How can one intercept such routed events outside the window where it has been raised (if even possible)? If not possible, what is the usual way to handle that?
Routed Events dont from new windows to owners. RoutedCommands also wont work directly. But Binding
s can work!
When you set childWindow.OwnerWindow = Application.Current.MainWindow
the childWindow gets logically connected to the MainWindow via its OwnerWindow
property.
You could use that to bind to OwnerWindow
's ViewModel command and execute that.
<Window x:Class="...ChildWindow"
... >
<Button Command="{Binding Path=OwnerWindow.DataContext.SaveCommand,
RelativeSource={RelativeSource
AncestorType={x:Type Window}}}"
Content="Execute Owner's Save Command" />
</Window>
这篇关于处理从主窗口中的模态对话框引发的冒泡事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!