【问题标题】:How can I block event in root element and let the event fired in the container(parent) level?如何阻止根元素中的事件并让事件在容器(父)级别触发?
【发布时间】:2014-12-04 09:17:09
【问题描述】:

我正在使用 WPF。 我的视觉树层次结构如下: RadDiagram > RadDiagramShape > MyControl RadDiagramShape 的内容是 mycontrol。

在 myControl 类中,我处理了 (MouseLeftButtonDown) 事件,我将拖放代码放入其中(我需要在另一个地方使用它)。所以,这是直接事件而不是隧道或冒泡!

当我移动作为 RadDiagramShape 内容的自定义控件时,在 RadDiagram 中,它不会移动(它试图被拖动),因为 MouseLeftButtonDown 已在 MyControl 中处理。 它防止事件在可视树中冒泡,防止任何父处理程序收到事件通知。

我尝试将 RadDiagramShape 和 RadDiagram 的事件处理为 e.Handled = true; 但它什么也没做,因为它是 MouseLeftButtonDown 并且它是在根元素内部处理的,所以它不会冒泡或隧道,而且我没有覆盖移动代码,我不想覆盖它。因为我之前尝试过,但它并没有像 WPF 中内置的那样给我带来同样的轻微移动。

如何在根元素中阻止 MouseLeftButtonDown 事件并让该事件在容器(父)级别触发?

【问题讨论】:

    标签: wpf events event-handling mouseleftbuttondown


    【解决方案1】:

    如果 'OriginalSource' 与您想要的控件相同,请检查您的控件处理程序。

    if ((e.OriginalSource is TextBox) && (e.OriginalSource as TextBox).Name == "TextBoxName")
                {
                    //Do every thing you want            
                }

    【讨论】:

      【解决方案2】:

      谢谢莱拉·卡里米。你给了我方向,条件本身不起作用。但我提出了另一个条件,它起作用了

              void MyLabel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
              {
                  MyLabel dc = (MyLabel)sender;
                  if (dc.Parent.GetType() != typeof(RadDiagramShape))
                  {///....drop it}
      }
      

      【讨论】:

        猜你喜欢
        • 2010-11-02
        • 1970-01-01
        • 2011-07-03
        • 1970-01-01
        • 2013-08-26
        • 1970-01-01
        • 2015-12-18
        • 2023-03-28
        • 2010-11-25
        相关资源
        最近更新 更多