受父母影响的FindElementsInHostCoordinates?

时间:2014-04-18 09:55:47

标签: wpf windows-phone-8

我正面临以下问题。我尝试在两个网格之间实现拖放,并找出我要​​删除的元素移动的位置我使用VisualTreeHelper.FindElementsInHostCoordinates。但是我的Element是孩子的网格总是作为元素返回到我的接触点。这是守则。谢谢你的帮助。

    private void Start_Drag(object sender, ManipulationStartedEventArgs e)
    {
        e.ManipulationContainer = LayoutRoot;
        Anchor.X = e.ManipulationOrigin.X;
        Anchor.Y = e.ManipulationOrigin.Y;

        (sender as UIElement).RenderTransform = new TranslateTransform();

        (sender as FrameworkElement).Parent.SetValue(Canvas.ZIndexProperty, 50);
    }

    private void Do_Drag(object sender, ManipulationDeltaEventArgs e)
    {
        var FinalPoint = new Point(e.CumulativeManipulation.Translation.X + Anchor.X, e.CumulativeManipulation.Translation.Y + Anchor.Y + 28);
        Debug.WriteLine("cumulative_drag ({0},{1})", FinalPoint.X, FinalPoint.Y);

        var Elements = VisualTreeHelper.FindElementsInHostCoordinates(FinalPoint, LayoutRoot);
        Debug.WriteLine("({0},{1})", Elements.Contains(SourceGrid), Elements.Contains(CoursesGrid));

        ((sender as UIElement).RenderTransform as TranslateTransform).X = e.CumulativeManipulation.Translation.X;
        ((sender as UIElement).RenderTransform as TranslateTransform).Y = e.CumulativeManipulation.Translation.Y;
    }

    private void End_Drag(object sender, ManipulationCompletedEventArgs e)
    {
        var FinalPoint = new Point(e.TotalManipulation.Translation.X + Anchor.X, e.TotalManipulation.Translation.Y + Anchor.Y + 42);
        Debug.WriteLine("end_drag ({0},{1})", FinalPoint.X, FinalPoint.Y);
        var Elements = VisualTreeHelper.FindElementsInHostCoordinates(FinalPoint, this);
        var IsOverSource = Elements.Contains(SourceGrid);
        var IsOverCourses = Elements.Contains(CoursesGrid);
        (sender as UIElement).RenderTransform = null;

        //push to background
        (sender as FrameworkElement).Parent.SetValue(Canvas.ZIndexProperty, -50);
    } 

1 个答案:

答案 0 :(得分:0)

请查看此链接Drag and drop from list to canvas on windows phone with MVVM

基本上你可以改用变换。这样您就可以将元素的点转换为两个不同网格上的位置,然后检查位置,如果它的负数或太大,您可以使用logik来定义它的位置。