温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

基于C#的wpf怎么实现Grid内控件拖动

发布时间:2021-11-20 16:41:50 来源:亿速云 阅读:212 作者:iii 栏目:开发技术

这篇文章主要介绍“基于C#的wpf怎么实现Grid内控件拖动”,在日常操作中,相信很多人在基于C#的wpf怎么实现Grid内控件拖动问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”基于C#的wpf怎么实现Grid内控件拖动”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

    前言:

    有一些业务场景中我们需要拖动控件,在Grid中就可以实现控件拖动,通过设置Margin属性即可,根据鼠标的移动,设置相应的MarginLeft、Top,当然有时也不是直接设置的,需要根据HorizontalAlignmentVerticalAlignment值有不同的计算方法。

    一、如何实现?

    1.注册鼠标事件

    拖动的控件需要注册3个鼠标事件分别是,鼠标按下、鼠标移动、鼠标弹起。

    以Button为例:

    <Button   PreviewMouseDown="Button_MouseDown" 
              PreviewMouseMove="Button_MouseMove" 
              PreviewMouseUp="Button_MouseUp"> </Button>

    2.记录位置

    在鼠标按下事件中记录位置。

    //鼠标是否按下
    bool _isMouseDown = false;
    //鼠标按下的位置
    Point _mouseDownPosition;
    //鼠标按下控件的Margin
    Thickness _mouseDownMargin;
    //鼠标按下事件
    private void Button_MouseDown(object sender, MouseButtonEventArgs e)
    {
     
        var c = sender as Control;
        _isMouseDown = true;
        _mouseDownPosition = e.GetPosition(this);
        _mouseDownMargin = c.Margin;
    }

    3.跟随鼠标移动

    鼠标按下后移动鼠标,控件需要跟随鼠标移动。根据HorizontalAlignmentVerticalAlignment值不同,计算Margin的方式也不同。

    private void Button_MouseMove(object sender, MouseEventArgs e)
    {
     
        if (_isMouseDown)
        {
     
            var c = sender as Control;
            var pos = e.GetPosition(this);
            var dp = pos - _mouseDownPosition;
            double left, top, right, bottom;
            if (c.HorizontalAlignment == HorizontalAlignment.Stretch|| c.HorizontalAlignment == HorizontalAlignment.Center)
            //中央移动距离是双倍
            {
     
                left= _mouseDownMargin.Left+ dp.X * 2;
                right = _mouseDownMargin.Right;
            }
            else if(c.HorizontalAlignment== HorizontalAlignment.Left)
            //左边是正常距离
            {
     
                left = _mouseDownMargin.Left + dp.X ;
                right = _mouseDownMargin.Right;
            }
            else
            //右边是右边距距离
            {
     
                left = _mouseDownMargin.Left;
                right = _mouseDownMargin.Right - dp.X;
            }
            if (c.VerticalAlignment == VerticalAlignment.Stretch || c.VerticalAlignment == VerticalAlignment.Center)
            //中央移动距离是双倍
            {
     
                top = _mouseDownMargin.Top+ dp.Y* 2;
                bottom = _mouseDownMargin.Bottom;
            }
            else if (c.VerticalAlignment == VerticalAlignment.Top)
            //顶部是正常距离
            {
     
                top = _mouseDownMargin.Top + dp.Y ;
                bottom = _mouseDownMargin.Bottom;
            }
            else
            //底部是底边距距离
            {
     
                top = _mouseDownMargin.Top ;
                bottom = _mouseDownMargin.Bottom- dp.Y;
            }
            c.Margin = new Thickness(left, top, right, bottom);
        }
    }

    4.恢复标识

    鼠标弹起后需要恢复标识,让控件不再跟随鼠标移动。

    private void Button_MouseUp(object sender, MouseButtonEventArgs e)
    {
     
        if (_isMouseDown)
        {
     
            _isMouseDown = false;
            //移动了的控件不响应点击事件(此处根据具体需求)
            e.Handled = true;
        }
    }

    二、示例

    示例代码:

    <Window x:Class="WpfControlMove.MainWindow"
            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:WpfControlMove"
            mc:Ignorable="d"
            Title="MainWindow" Height="360" Width="640">
        <Grid>
            <Button Width="120" Height="50" Content="移动"   PreviewMouseDown="Button_MouseDown" PreviewMouseMove="Button_MouseMove" PreviewMouseUp="Button_MouseUp"> </Button>
        </Grid>
    </Window>

    效果预览:

    基于C#的wpf怎么实现Grid内控件拖动

    到此,关于“基于C#的wpf怎么实现Grid内控件拖动”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

    向AI问一下细节

    免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

    AI