【问题标题】:Tunneling Commands隧道命令
【发布时间】:2012-06-20 08:18:07
【问题描述】:

我有几个在 ListBox 中实例化的用户控件。我的窗口中有一个按钮,它会引发NavigationCommands.Refresh,还有一个CanExecute 处理程序允许它执行:

<Window.CommandBindings>
    <CommandBinding Command="NavigationCommands.Refresh" CanExecute="CanAlwaysExecute" />
</Window.CommandBindings>

在每个动态创建的用户控件(在 ListBox 的 ItemTemplate 中创建)中,我都有一个用于 NavigationCommands.Refresh 的处理程序。当我单击窗口中的按钮时,我在每个用户控件中设置的处理程序是否应该不触发?我需要命令来隧道到每个 UC,但它不是。

<UserControl.CommandBindings>
    <CommandBinding Command="NavigationCommands.Refresh" Executed="UpdateStatus" CanExecute="CanAlwaysExecute" />
</UserControl.CommandBindings>
....
<Button Width="200" Text="Refresh All" Command="NavigationCommands.Refresh"/>

【问题讨论】:

    标签: wpf icommand routed-events commandbinding tunneling


    【解决方案1】:

    隧道命令不是这样工作的,它们从根到目标站点,它们不会“分裂”。所以它们从窗口开始,到被点击的按钮结束。

    【讨论】:

    • @Echilon:要有创意。例如如果单击主按钮,则为所有感兴趣的控件手动发出命令。
    • 这将需要遍历列表框中的所有控件,找到嵌套在其中的另一个列表框,然后遍历其中的所有项目并找到我的用户控件,然后调用一个方法。有没有更优雅的方式?
    • @Echilon:我不知道。但是,您不需要遍历 UI,在您的模型中,这些关联应该更加清晰,并且命令所做的任何更改都应该反映在 UI 中。循环数据应该没什么大不了的。
    猜你喜欢
    • 2015-05-11
    • 2020-10-26
    • 1970-01-01
    • 2019-01-22
    • 1970-01-01
    • 2019-06-20
    • 2014-09-04
    • 2020-06-17
    • 2020-08-07
    相关资源
    最近更新 更多