Click here to Skip to main content
15,885,869 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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.

C#
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
Updated 1-Jan-13 19:48pm
v2

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[^]
 
Share this answer
 
Can anyone please help me with this code? I am running behind time.
Thanks
 
Share this answer
 
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?
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900