【问题标题】:WPF Routed Command TunnelingWPF 路由命令隧道
【发布时间】:2015-05-11 20:29:06
【问题描述】:

我想在我的 WPF 应用程序中实现一个路由到用户控件的路由命令。

我的理解是,路由命令(如路由事件)可以在可视化树中上下传播以找到合适的处理程序,但是我找不到如何执行此操作的简单示例。

我在创建路由命令时没有问题,该命令在定义它的同一可视树节点中处理。

请有人提供一个简单的解释或示例,演示我如何连接路由命令以在可视树下方的节点中处理。

更新: 我的用例是我想在选项卡项中执行一些功能,实现为用户控件,我想通过键盘输入触发它。因此,我需要在主窗口中处理键盘输入,然后将命令传送到我的特定选项卡项。

谢谢

【问题讨论】:

    标签: c# wpf routed-commands


    【解决方案1】:

    对于路由命令,由于与需要预览的 CanExecute 紧密耦合,我认为您不能这样做。

    在 WPF 中,考虑将EventManager 用于路由事件。

    在那里,您可以将路由定义为 Bubbling、Tunneling 或 Direct。

    示例:

    public static readonly RoutedEvent ButtonColorChangedEvent = EventManager.RegisterRoutedEvent("ButtonColorChanged",RoutingStrategy.Bubble,typeof(DependencyPropertyChangedEventHandler),typeof(Shirt));
    
    public event RoutedEventHandler ButtonColorChanged  {
        add {AddHandler(ButtonColorChangedEvent,value);}
        remove { RemoveHandler(ButtonColorChangedEvent, value); }
    }
    

    【讨论】:

    【解决方案2】:

    路由从根开始到焦点元素隧道 如果没有指定 cmd 目标,然后从焦点 elmt 冒泡到根目录。

    经过挖掘和搜索,我得到了解决方案: 您必须设置“隧道停止和冒泡开始元素” 自己通过“CommandTarget”

    public partial class Grid_Dienste : UserControl {
    
        public static Grid_Dienste   current     { get; set; }
    
        public Grid_Dienste()
        {
    
            current = this;
    
            ...
    
        // this is the prerequsite, works on singeltons
    

    在上层节点/元素的 xaml 中,您可以引用“当前”

    Command                     = "{Binding cmdLoad}"
    CommandTarget               = "{x:Static local:Grid_Dienste.current}"
    

    />所以结论:隧道并不意味着从焦点到最底层 视觉,但从根到焦点。

    【讨论】:

      猜你喜欢
      • 2014-06-22
      • 2012-06-20
      • 1970-01-01
      • 2013-12-02
      • 2015-03-14
      • 2010-09-26
      • 1970-01-01
      • 1970-01-01
      • 2011-05-27
      相关资源
      最近更新 更多