Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#WPF
Hi,
 
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;
                CnvsFloorPlan.Children.Add(lbl);
                i++;
                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"   />
                   </Grid>
            </Canvas>
        </ScrollViewer>
 

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
good
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:
 
<resourcedictionary>
            <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" />
                </stackpanel>
            </controltemplate>
        </resourcedictionary>
 
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.ApplyTemplate();
                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
                CnvsFloorPlan.Children.Add(TmbRoom);
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.
  Permalink  
Rate this: bad
good
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
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 840
1 OriginalGriff 407
2 Abhinav S 303
3 Manas Bhardwaj 285
4 thatraja 210
0 Sergey Alexandrovich Kryukov 7,967
1 OriginalGriff 4,332
2 Peter Leow 3,699
3 Maciej Los 3,515
4 Er. Puneet Goel 3,107


Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 20 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid