Drag Items from a List and Drop Them on a Canvas : Drag Drop « Windows Presentation Foundation « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » Windows Presentation Foundation » Drag Drop 
24.79.1.Drag Items from a List and Drop Them on a Canvas
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF " Height="300" Width="300">
    <DockPanel LastChildFill="True" >
        <ListBox DockPanel.Dock="Left" Name="lstLabels">
            <ListBox.Resources>
                <Style TargetType="{x:Type ListBoxItem}">
                    <EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListBoxItem_PreviewMouseLeftButtonDown"/>    
                    <EventSetter Event="PreviewMouseMove" Handler="ListBoxItem_PreviewMouseMove"/>
                </Style>
            </ListBox.Resources>
            <ListBoxItem>A</ListBoxItem>
            <ListBoxItem>B</ListBoxItem>
            <ListBoxItem>C</ListBoxItem>
            <ListBoxItem>D</ListBoxItem>
            <ListBoxItem>E</ListBoxItem>
            <ListBoxItem>F</ListBoxItem>
            <ListBoxItem>G</ListBoxItem>
            <ListBoxItem>H</ListBoxItem>
        </ListBox>
        <Canvas AllowDrop="True" Background="Red"
                DragEnter="cvsSurface_DragEnter" Drop="cvsSurface_Drop" 
                Name="cvsSurface" >
        </Canvas>
    </DockPanel>
</Window>
//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        private ListBoxItem draggedItem;
        private Point startDragPoint;

        public Window1()
        {
            InitializeComponent();
        }
        private void cvsSurface_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Text))
            {
                e.Effects = DragDropEffects.Copy;
            }
            else
            {
                e.Effects = DragDropEffects.None;
            }
        }
        private void cvsSurface_Drop(object sender, DragEventArgs e)
        {
            Label newLabel = new Label();
            newLabel.Content = e.Data.GetData(DataFormats.Text);

            cvsSurface.Children.Add(newLabel);
            Canvas.SetLeft(newLabel,100);
            Canvas.SetTop(newLabel, 200);
        }
        private void ListBoxItem_PreviewMouseLeftButtonDown(object sender,MouseButtonEventArgs e)
        {
            draggedItem = sender as ListBoxItem;
            startDragPoint = e.GetPosition(null);
        }
        private void ListBoxItem_PreviewMouseMove(object sender,MouseEventArgs e)
        {
            Point position = e.GetPosition(null);
            DragDrop.DoDragDrop(draggedItem, draggedItem.Content,DragDropEffects.Copy);
        }
    }
}
WPF Drag Items From A List And Drop Them On A Canvas
24.79.Drag Drop
24.79.1.Drag Items from a List and Drop Them on a CanvasDrag Items from a List and Drop Them on a Canvas
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.