none
Hacer drag and drop a través de canvas. RRS feed

  • Pregunta

  • Tengo el siguiente código para mover controles dentro de una canva pero logro moverlos de una canva a otra. alguien sabría como hacerlo?

    XAML

    <Window x:Class="DragAndDropApp.DragAndDropControls"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Drag and Drop Window"
            Width="782" Height="376">
        <Grid>
            <Canvas Margin="20,20,0,0" Background="#FFEBEBEB" Name="DesigningCanvas" HorizontalAlignment="Left" Width="433" Height="297" VerticalAlignment="Top">
                <TextBox Text="It's a TextBox control" Canvas.Left="50" Canvas.Top="30" Width="177" />
                <Label Content="It's a Label control" Canvas.Left="50" Width="110"/>
                <Button Content="It's a Button control" Canvas.Left="50" Canvas.Top="60"/>
                <RadioButton Canvas.Left="50" Canvas.Top="90" Height="30" Name="radioButton1">
                    <RadioButton.Content>
                        <StackPanel Orientation="Horizontal">
                            <Label Content="It's a Custom RadioButton Control with CheckBox control"/>
                            <CheckBox Margin="7"/>
                        </StackPanel>
                    </RadioButton.Content>
                </RadioButton>
                <CheckBox Canvas.Left="50" Canvas.Top="120
                          " Content="It's a CheckBox control" Height="16" Name="checkBox1" />
                <ListBox Canvas.Left="48" Canvas.Top="159" Height="100" Name="listBox1" Width="151">
                    <ListBoxItem Content="It's a ListBox control" />
                </ListBox>
            </Canvas>
        <Canvas Height="297" HorizontalAlignment="Left" Margin="480,20,0,0" Name="canvaSecundaria" VerticalAlignment="Top" Width="259" Background="#FFEBEBEB"></Canvas>
        </Grid>
    </Window>

    C#

    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 DragAndDropApp
    {
        public partial class DragAndDropControls : Window
        {
            double FirstXPos, FirstYPos, FirstArrowXPos, FirstArrowYPos;
            object MovingObject;

            public DragAndDropControls()
            {
                InitializeComponent();
              
                foreach (Control control in DesigningCanvas.Children)
                {
                    control.PreviewMouseLeftButtonDown += this.MouseLeftButtonDown;
                    control.PreviewMouseMove += this.MouseMove;
                    control.PreviewMouseLeftButtonUp += this.PreviewMouseLeftButtonUp;
                    control.Cursor = Cursors.Hand;
                }

              

                // Setting up the Lines that we want to show the path of movement
                List<Double> Dots = new List<double>();
                Dots.Add(1);
                Dots.Add(2);
            }

            void PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
            {
                MovingObject = null;
            }

            private void MouseMove(object sender, MouseEventArgs e)
            {
                if (e.LeftButton == MouseButtonState.Pressed &&
                    sender == MovingObject)
                {
                    
                    (sender as Control).SetValue(Canvas.LeftProperty,
                        e.GetPosition((sender as Control).Parent as Control).X - FirstXPos - 20);

                    (sender as Control).SetValue(Canvas.TopProperty,
                        e.GetPosition((sender as Control).Parent as Control).Y - FirstYPos - 20);
                }
            }

            private void MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                FirstXPos = e.GetPosition(sender as Control).X;
                FirstYPos = e.GetPosition(sender as Control).Y;
                FirstArrowXPos = e.GetPosition((sender as Control).Parent as Control).X - FirstXPos - 20;
                FirstArrowYPos = e.GetPosition((sender as Control).Parent as Control).Y - FirstYPos - 20;
                MovingObject = sender;
            }
        }
    }

    miércoles, 19 de diciembre de 2012 14:24