处理从主窗口中的模态对话框引发的冒泡事件 [英] Handling a bubbling event raised from a modal dialog in main window

查看:186
本文介绍了处理从主窗口中的模态对话框引发的冒泡事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的WPF应用程序中有一个模态对话框,允许用户登录到服务器。该对话框只包含一个用户控件,用于处理所有登录信息(UI,Web服务调用,以及在调用返回时提出路由事件)。



我可以在对话框中处理我的事件(在登录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 Bindings 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆