WPF路由命令和ShowDialog Windows [英] WPF Routed Commands and ShowDialog Windows
问题描述
我想知道通过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?
谢谢!
Jon
Thanks! Jon
推荐答案
我们在应用程序中遇到这种情况。我们使用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中:
DialogWindow windowB = new DialogWindow();
windowB.Owner = this;
windowB.ShowDialog();
然后,在DialogWindow中,执行命令的所有控件都将其CommandTarget绑定到父窗口的Owner:
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>
或者,如果您从Window B中的代码执行:
Or alternatively, if you're executing from code inside Window B:
SomeCommand.Execute(params, this.Owner);
CommandTarget将允许Window A监听CanExecute和Execute。
CommandTarget will let Window A listen for CanExecute and Execute.
这篇关于WPF路由命令和ShowDialog Windows的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!