Click here to Skip to main content
12,455,917 members (49,908 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# WPF
Hi,

I would like to get some help in creating draggable stack panel. When a combobox item is selected, I have to automatically populate an image and several draggable stack panels which contain an image and a label. These stack panels should be dropped over the image.

Can anyone please help me with this. I have tried a lot of ways, which never really worked.

Thanks in advance

EDIT:

Hi db7uk,

Thank you for the answer, but I have tried all those links. They are about dragging listview items. I want to drag and drop a stackpanel over an image. In fact, there will be several stack panels, which I want to drag and drop over the image.

private void CmbFloorLevel_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string rootDic = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), @"\Images\UploadFiles");
            string[] selecteditem = CmbFloorLevel.SelectedItem.ToString().Split('.');
            roomsList = fpDB.getRooms(selecteditem[0].ToString().Trim());
            //rmIcon = new RoomIcon[roomsList.Count];
            string filename = getFloorplan();
            if (filename != string.Empty)
            {
                ImgFloorPlan.Source = new BitmapImage(new Uri(rootDic + "\\" + filename));
 
            }
            else
            {
                ImgFloorPlan.Source = new BitmapImage(new Uri(System.IO.Path.GetFullPath("Images/blank.png")));
            }
            ImgFloorPlan.BringIntoView();
            loadRooms();
        }
 
 public void loadRooms()
        {
            //Removing all previous canvas labels
            int i;
            for (int index = CnvsFloorPlan.Children.Count - 1; index >= 0; index--)
            {
                if (CnvsFloorPlan.Children[index] is Label)
                {
                    CnvsFloorPlan.Children.RemoveAt(index);
                }
            }
 
            for (int index = GrdFloorPlan.Children.Count - 1; index >= 0; index--)
            {
                if (GrdFloorPlan.Children[index] is Label)
                    GrdFloorPlan.Children.RemoveAt(index);
            }
 
            i = 0;
            StkRooms = new StackPanel();
            StkRooms.Name = "StkRooms" + i.ToString();
            StkRooms.Background = Brushes.Gray;
            StkRooms.Margin = new Thickness(2);
            StkRooms.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
 
            foreach (string room in roomsList)
            {
                lbl = new Label();
                lbl.Content = room;
                lbl.Foreground = Brushes.White;
                lbl.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                lbl.Background = Brushes.Gray;
                lbl.Margin = new Thickness(2);
                lbl.PreviewMouseDown += new MouseButtonEventHandler(StkRooms_MouseDown);
                lbl.PreviewMouseMove += new MouseEventHandler(StkRooms_MouseMove);
                lbl.Cursor = Cursors.Hand;
                lbl.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                StkRooms.Children.Add(lbl);
                i++;
            }
            CnvsFloorPlan.Children.Add(StkRooms);
        }
 
private void StkRooms_MouseDown(object sender, MouseButtonEventArgs e)
        {
            dragStarted = true;
            if (e.Source == lbl)
            {
                DragDrop.DoDragDrop(lbl, lbl, DragDropEffects.Move);
            }
            base.OnPreviewMouseDown(e);
        }
 
        private void StkRooms_MouseMove(object sender, MouseEventArgs e)
        {
            if (dragStarted && sender is Label)
            {
 
            }
        }
 
        private void ImgFloorPlan_Drop(object sender, DragEventArgs e)
        {
            Label l = e.Data.GetData(typeof(Label)) as Label;
            //l.PreviewMouseDown += new MouseButtonEventHandler(StkRooms_MouseDown);
            //l.Cursor = Cursors.Hand;
            //l.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            l.Background = Brushes.Gray;
            l.Height = 25;
 
            Window wnd = Window.GetWindow(this);
            Point mousepos = Mouse.GetPosition(wnd);
            //MessageBox.Show(mousepos.X.ToString() + "," + mousepos.Y.ToString());
            l.Margin = new Thickness { Left = mousepos.X, Bottom = mousepos.Y };
 
            Panel element = l.Parent as Panel;
            element.Children.Remove(l);
            //CnvsFloorPlan.Children.Remove(l);
            GrdFloorPlan.Children.Add(l);
        }


Somehow whenever i populate the stackpanels, the image doesn't show. And when i try to drop the stackpanel at a particular location, it gets dropped at a different location. I do not know what I am doing wrong.
Can anyone please help me with this.

Thanks in advance
Posted 1-Jan-13 8:31am
Updated 1-Jan-13 19:48pm
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Some excellent drag drop examples.

Not 100 % sure what your initial requirement is though. Could you improve answer with some things you have tried?

Drag and Drop in WPF[^]
Drag and Drop in WPF - Part II[^]
http://code.google.com/p/gong-wpf-dragdrop/[^]
Drag and Drop Items in a WPF ListView[^]
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Can anyone please help me with this code? I am running behind time.
Thanks
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Drag and drop in wpf can be done easily using Thumbs as explained here:
how to drag and drop multiple labels over image in wpf from codebehind?
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160826.1 | Last Updated 20 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100