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());
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()
{
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.Background = Brushes.Gray;
l.Height = 25;
Window wnd = Window.GetWindow(this);
Point mousepos = Mouse.GetPosition(wnd);
l.Margin = new Thickness { Left = mousepos.X, Bottom = mousepos.Y };
Panel element = l.Parent as Panel;
element.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