WPF 路由命令和 ShowDialog 窗口 [英] WPF Routed Commands and ShowDialog Windows

查看:28
本文介绍了WPF 路由命令和 ShowDialog 窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在可视树中处理通过 ShowDialog 打开的窗口.

I was wondering how a window opened through ShowDialog is treated in the Visual Tree.

例如,窗口 A 通过 ShowDialog 打开窗口 B.在窗口 B 上触发路由命令时,窗口 A 不会收到该命令(即使它正在侦听它).

For example, Window A opens Window B through a ShowDialog. When firing a routed command on Window B, Window A does not receieve the command (even though it is listening for it).

有什么办法可以做到吗?

Is there any way to make this work?

谢谢!乔恩

推荐答案

我们在我们的应用程序中一直遇到这种情况.我们为此使用 Window.Owner 属性和 ICommandSource.CommandTarget 属性.

We run into this situation all the time in our application. We use the Window.Owner property and ICommandSource.CommandTarget property for this.

例如,在窗口 A 中:

For example, in Window A:

DialogWindow windowB = new DialogWindow();
windowB.Owner = this;
windowB.ShowDialog();

然后,在 DialogWindow 中,所有执行命令的控件都将其 CommandTarget 绑定到父窗口的所有者:

Then, in DialogWindow, all the controls that execute commands have their CommandTarget bound to the parent Window's Owner:

<Window x:Class="DialogWindow" x:Name="wnd">
    <Button Command="SomeCommand" CommandTarget="{Binding Owner, ElementName=wnd}"/>
</Window>

或者,如果您从窗口 B 中的代码执行:

Or alternatively, if you're executing from code inside Window B:

SomeCommand.Execute(params, this.Owner);

CommandTarget 会让窗口 A 监听 CanExecute 和 Execute.

CommandTarget will let Window A listen for CanExecute and Execute.

这篇关于WPF 路由命令和 ShowDialog 窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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