Click here to Skip to main content
13,045,379 members (117,556 online)
Rate this:
Please Sign up or sign in to vote.
See more:

I am generating a set of labels from codebehind in wpf, C#. I can drag and drop the last label only once. I am not able to drag and drop any other labels.

The C# code is as follows:

ImgFloorPlan.Source = new BitmapImage(new Uri(rootDic + "\\" + filename));
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 { Left = 5, Top = j };
                lbl.MouseDown += new MouseButtonEventHandler(StkRooms_MouseDown);
                lbl.MouseMove += new MouseEventHandler(StkRooms_MouseMove);
                lbl.Cursor = Cursors.Hand;
                lbl.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                j += 30;

All the above labels are added to a canvas. ImgFloorPlan is also added to the canvas. All the above labels should be dragged and dropped over the image.

XAML code is as follows:
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Margin="26,63,98,185" x:Name="_scrollViewer">
            <Canvas x:Name="CnvsFloorPlan">
                   <Grid x:Name="GrdFloorPlan" HorizontalAlignment="Left" VerticalAlignment="Top" Width="{Binding ElementName=CnvsFloorPlan, Path=ActualWidth}" Height="{Binding ElementName=CnvsFloorPlan, Path=ActualHeight}" >
                         <Image x:Name="ImgFloorPlan" Source="Images/blank.png" HorizontalAlignment="left" VerticalAlignment="Top" AllowDrop="True" Drop="ImgFloorPlan_Drop"  Visibility="Visible"   />

Can anyone help me with this please? I have spent more than 3 weeks trying to fix this. I just have to populate multiple labels depending on the condition. and user should be able to drag and drop these labels over the image. Please help me with this.

Thanks in advance
Posted 5-Jan-13 9:18am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Probably the only trick I can think of is to use mouse capture, so that once you start dragging, the mouse events keep going to the control. I think it's a static property of the Mouse class in WPF, so type Mouse. and you should see a method you can use to make the mouse events keep going to your label. Then dragging it until the mouse button lifts becomes trivial. Don't forget to clear the capture when you're done
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Thank you all for your solutions. HOwever, I have used Thumbs to solve this issue and it works quite well.

We have to first create a control template for the item to be dragged as follows:

            <controltemplate x:key="rmIcon" targettype="Thumb" xmlns:x="#unknown">
                <stackpanel x:name="StkRoom" horizontalalignment="Center">
                    <image x:name="tplImage" source="Images\lightoffsmall.png"></image>
                    <textblock x:name="tplTextBlock" text="User stage" foreground="White" />

Then I created the necessary draggable thumbs from codebehind as follows:
TmbRoom = new Thumb();
                TmbRoom.Margin = new Thickness { Left = left, Top = top };
                TmbRoom.Template = this.Resources["rmIcon"] as ControlTemplate;
                TmbRoom.DragDelta += new DragDeltaEventHandler(onDragDelta);
                TmbRoom.PreviewMouseDown += TmbRoom_PreviewMouseDown;
                TmbRoom.PreviewMouseUp += TmbRoom_PreviewMouseUp;
                //changing text in the template
                TextBlock txt = (TextBlock)TmbRoom.Template.FindName("tplTextBlock", TmbRoom);
                txt.Text = room;
                // Put newly created thumb on the canvas
private void onDragDelta(object sender, DragDeltaEventArgs e)
            Thumb thumb = e.Source as Thumb;
            double left = Canvas.GetLeft(thumb) + e.HorizontalChange;
            double top = Canvas.GetTop(thumb) + e.VerticalChange;
            Canvas.SetLeft(thumb, left);
            Canvas.SetTop(thumb, top);
            //LblStatus.Content = left.ToString() + ", " + top.ToString();

The onDragdelta function enables dragging and is easier to handle compared to conventional dragging methods in wpf.

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.170713.1 | Last Updated 20 Jan 2013
Copyright © CodeProject, 1999-2017
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