2016-05-13 50 views
1

我在我的用戶控件(WorkflowContainer)中創建了一個名爲IsDragMouseOver的新布爾值。如何檢查在wpf中UserControl的範圍內拖動?

public static DependencyProperty IsDragMouseOverProperty = DependencyProperty.Register("IsDragMouseOver", typeof(bool), typeof(WpfWorkflowBaseDesigner), null); 

    public bool IsDragMouseOver 
    { 
     get 
     { 
      return (bool)GetValue(IsDragMouseOverProperty); 
     } 
     set 
     { 
      SetValue(IsDragMouseOverProperty, value); 
     } 
    } 


    protected override void OnDragEnter(DragEventArgs e) 
    { 
     base.OnDragEnter(e); 
     IsDragMouseOver = true; 
    } 

    protected override void OnDragOver(DragEventArgs e) 
    { 
     base.OnDragOver(e); 
     IsDragMouseOver = true; 
    } 


    protected override void OnDrop(DragEventArgs e) 
    { 
     base.OnDrop(e); 
     IsDragMouseOver = false; 
    } 

    protected override void OnDragLeave(DragEventArgs e) 
    { 
     base.OnDragLeave(e); 
     IsDragMouseOver = false; 
    } 

我想,當我在用戶控件的範圍拖動到顯示的藍色標誌加用戶控件的子元素: enter image description here

是做工精細只有我拖過對用戶的控制,但是當我拖過子元素IsDragMouseOver返回false和藍色標誌加上沒有顯示:子元素的 enter image description here

XAML代碼:

<Path Stretch="Fill" Width="12" Height="12" VerticalAlignment="Center" HorizontalAlignment="Center" StrokeThickness="0.5" Stroke="{DynamicResource BorderBrush}" Panel.ZIndex="1" Fill="Green" Margin="0 0 2 0" 
      Visibility="{Binding IsDragMouseOver, RelativeSource={RelativeSource AncestorType={x:Type wfb:Workflow}}, Converter={StaticResource BoolToVisibilityConverter}}" 
      Data="M4.1561281,2.2702953 L4.8524521,2.2702954 4.8509674,3.963097 5.8969377,3.9630803 5.8969378,5.0916036 4.8524628,5.1061913 4.8524521,6.7843885 4.1561281,6.7843887 4.1559771,5.0877741 3.1116421,5.0916036 3.1116421,3.9630803 4.1556735,3.9654722 4.1561281,2.2702953 z"/> 

如何在用戶控件的內部範圍內拖動時顯示符號加號,並且只在用戶控件的外部範圍上拖動時禁用符號加號?

+0

你試過設置[IsHitTestVisible = 「假」(http://stackoverflow.com/q/4226770/1997232)的孩子? – Sinatr

+0

@Sinatr,感謝您的回覆,我不會設置IsHitTestVisible =「false」,因爲它會失去所有其他的孩子事件,並且有那麼多子元素 –

+0

這就是我期望你會回答的。然後,您必須在孩子中處理鼠標事件(特別是拖動事件),以便爲用戶控制組合'IsDragMouseOver'結果。 – Sinatr

回答

0

手柄dragEnter事件的子元素,代碼示例:

protected override void OnDragEnter(DragEventArgs e) 
     { 
      base.OnDragEnter(e); 
      WorkflowContainer workflowContainer = DesignerHelper.FindAncestor<WorkflowContainer>(e.OriginalSource as DependencyObject); 
      if (workflowContainer != null) 
      { 
       workflowContainer.IsDragMouseOver = true; 
      } 
     }