2012-10-03 2 views
0

저는 캔버스에 GraphNodes를 그려서 캔버스에 ContentControls로 추가합니다. 모든 그래프 노드는 노드에서 다른 노드로 연결 선을 그리는 데 사용하는 adorner를가집니다.WPF 마우스 위치에서 시각적으로 가져 오기

protected override void OnMouseUp(System.Windows.Input.MouseButtonEventArgs e) 
{ 
    var SourceNode = AdornedElement; 
    Point pt = PointToScreen(Mouse.GetPosition(this)); 
    var DestinationNode = ??? 
} 

내가 초기 GraphNode입니다 AdornedElement에서의 선을 그어야하기 시작 곳에서 소스 노드가이 시점에서 다음 adorner는 방법 onMouseUp에 있습니다. 또한 마우스를 놓은 좌표가 있습니다. 이 시점에서 또 다른 GraphNode가 있습니다. 이 시점에 해당하는 노드를 찾는 방법은 무엇입니까?

감사합니다.

protected override void OnMouseUp(System.Windows.Input.MouseButtonEventArgs e) 
{ 
    Point pt = PointToScreen(Mouse.GetPosition(this)); 
    UIElement canvas = LogicalTreeHelper.GetParent(AdornedElement) as UIElement; 
    // This is important to get the mouse position relative to the canvas, otherwise it won't work 
    pt = canvas.PointFromScreen(pt); 
    VisualTreeHelper.HitTest(canvas, null, HitTestResultCallbackHandler, new PointHitTestParameters(pt));    
} 

를, 그리고, 그러나 hitTest 방법은 GraphNode를 찾을 수 :

+0

합니까 [e.OriginalSource] (http://msdn.microsoft.com/en-us/library/system.windows.routedeventargs.originalsource.aspx) 또는 [e.Source] (HTTP : // msdn.microsoft.com/en-us/library/system.windows.routedeventargs.source.aspx) help? – LPL

+0

아니요 항상 소스 인 AdornedElement입니다. MouseUpEvent를 생성하는 대상 노드를 찾고 싶습니다. – alexandrudicu

+1

그런 다음 여기를 살펴보십시오. [Visual Layer에서의 히트 테스트] (http://msdn.microsoft.com/en-us/library/ms752097.aspx) – LPL

답변

0

OK, 많은 연구 후에 나는 해결책을 발견했다. GraphNode는 사용자 정의 객체임을 기억하십시오.

public HitTestResultBehavior HitTestResultCallbackHandler(HitTestResult result) 
{ 
    if (result != null) 
    { 
     // Search for elements that have GraphNode as parent 
     DependencyObject dobj = VisualTreeHelper.GetParent(result.VisualHit); 
     while (dobj != null && !(dobj is ContentControl)) 
     { 
     dobj = VisualTreeHelper.GetParent(dobj); 
     } 
     ContentControl cc = dobj as ContentControl; 
     if (!ReferenceEquals(cc, null)) 
     { 
     IEnumerable<DependencyObject> dependencyObjects = cc.GetSelfAndAncestors(); 
     if (dependencyObjects.Count() > 0) 
      { 
       IEnumerable<ContentControl> contentControls = dependencyObjects.Where(x => x.GetType() == typeof(ContentControl)).Cast<ContentControl>(); 
       if (contentControls.Count() > 0) 
        { 
         ContentControl cControl = contentControls.FirstOrDefault(x => x.Content.GetType() == typeof(GraphNode)); 
         if (cControl != null) 
         { 
          var graphNode = cControl.Content as GraphNode; 
          if (!ReferenceEquals(graphNode, null)) 
          { 
           // Keep the result in a local variable 
           m_DestinationNode = graphNode; 
           return HitTestResultBehavior.Stop; 
          } 
         }       
        } 
       } 
      }    
     } 
    m_DestinationNode = null; 
    return HitTestResultBehavior.Continue; 
} 
관련 문제