拖放形状 [英] Drag and drop shapes

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

问题描述

我有一个StackPanel中的矩形形状,我想拖放使用WPF用鼠标在网格滴!
我很感激,如果有人能帮助我吗?
谢谢大家。

I have a rectangle shape in a stackpanel and i want to drag and drop in a grid with the mouse using WPF! I appreciate it if someone can help me? Thanks to everyone.

推荐答案

一个非常简单的实现如下。它只是处理鼠标按钮按下长方形的/上/移动事件,以便与鼠标移动一起定位。有没有错误检查和什么可以阻止用户从拖动矩形了画布,并留下它那里

A very simple implementation follows. It simply handles the Mouse button down/up/move events of the Rectangle in order to position it along with mouse movement. There is no error checking and nothing to prevent the user from dragging the rectangle off of the Canvas and leaving it there.

XAML:

<Window x:Class="WpfApplication6.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Canvas Name="canvas">
            <Rectangle
                Name="rect"
                Width="50"
                Height="50"
                Canvas.Left="0"
                Canvas.Top="0"
                Fill="Red"
                MouseLeftButtonDown="rect_MouseLeftButtonDown"
                MouseLeftButtonUp="rect_MouseLeftButtonUp"
                MouseMove="rect_MouseMove"
                />
        </Canvas>
    </Grid>
</Window>

代码背后:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication6
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private bool _isRectDragInProg;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void rect_MouseLeftButtonDown( object sender, MouseButtonEventArgs e )
        {
            _isRectDragInProg = true;
            rect.CaptureMouse();
        }

        private void rect_MouseLeftButtonUp( object sender, MouseButtonEventArgs e )
        {
            _isRectDragInProg = false;
            rect.ReleaseMouseCapture();
        }

        private void rect_MouseMove( object sender, MouseEventArgs e )
        {
            if( !_isRectDragInProg ) return;

            // get the position of the mouse relative to the Canvas
            var mousePos = e.GetPosition(canvas);

            // center the rect on the mouse
            double left = mousePos.X - (rect.ActualWidth / 2);
            double top = mousePos.Y - (rect.ActualHeight / 2);
            Canvas.SetLeft( rect, left );
            Canvas.SetTop( rect, top );
        }
    }
}

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

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