拖动WPF用户控件 [英] Dragging a WPF user control

查看:174
本文介绍了拖动WPF用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建移动用户控件

    <UserControl x:Class="Restaurant.Views.Managerer.TablePanel"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Restaurant.Helpers.Converter"
        mc:Ignorable="d"
        x:Name="root"
        MouseLeftButtonDown="root_MouseLeftButtonDown"
        MouseLeftButtonUp="root_MouseLeftButtonUp"
        MouseMove="root_MouseMove"    
        DataContext="{Binding RelativeSource={RelativeSource Self}}">
....



代码

code

Point anchorPoint;
        Point currentPoint;
        bool isInDrag = false;

        private void root_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var element = sender as FrameworkElement;
            anchorPoint = e.GetPosition(null);
            element.CaptureMouse();
            isInDrag = true;
            e.Handled = true;
        }

        private void root_MouseMove(object sender, MouseEventArgs e)
        {
            if (isInDrag)
            {
                var element = sender as FrameworkElement;
                currentPoint = e.GetPosition(null);

                var transform = new TranslateTransform
                                    {
                                        X = (currentPoint.X - anchorPoint.X),
                                        Y = (currentPoint.Y - anchorPoint.Y)
                                    };
                this.RenderTransform = transform;
                anchorPoint = currentPoint;
            }
        }

        private void root_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (isInDrag)
            {
                var element = sender as FrameworkElement;
                element.ReleaseMouseCapture();
                isInDrag = false;
                e.Handled = true;
            }
        }

如果改变代码

X = (currentPoint.X - anchorPoint.X),
Y = (currentPoint.Y - anchorPoint.Y)

X = (currentPoint.X),
Y = (currentPoint.Y)

我可以移动用户控件,但是鼠标和用户控件不匹配

I can move the usercontrol, but mouse and usercontrol do not match

推荐答案

早上好。我睡和能想到的)))

Good morning. I slept and can think )))

 private TranslateTransform transform = new TranslateTransform();
        private void root_MouseMove(object sender, MouseEventArgs e)
        {
            if (isInDrag)
            {
                var element = sender as FrameworkElement;
                currentPoint = e.GetPosition(null);

                transform.X += currentPoint.X - anchorPoint.X;
                transform.Y += (currentPoint.Y - anchorPoint.Y);
                this.RenderTransform = transform;
                anchorPoint = currentPoint;
            }
        }

这篇关于拖动WPF用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆