Forums - C# Corner

Forum guidelines


  • 1.4k
  • 22
  • 16.1k

C# Drag and Drop - DragDrop.DoDragDrop()

Mar 9 2012 12:12 PM

I am trying to drag and drop one label into another label (receiving label).  Both labels have been set up in wpf and the recieving label has been set AllowDrop="True" Drop="lblTarget_Drop".  The problem I am getting is,  I cannot get the content and background colour to transfer across to the receiving label e.g:

Background colour is blue and its content is the number 1.  How do you get both the content and the background colour to transfer into the receiving label.

Thanks for your time.



//C# Drag and Drop code

    public partial class MainWindow : Window
        public MainWindow()

        private void lblSource_MouseDown(object sender, MouseButtonEventArgs e)
            Label lbl = (Label)sender;
            DragDrop.DoDragDrop(lbl, lbl.Content, DragDropEffects.Copy);  
            DragDrop.DoDragDrop(lbl, lbl.Background, DragDropEffects.Copy);

        private void lblTarget_Drop(object sender, DragEventArgs e)
            SolidColorBrush scb1 = (SolidColorBrush)e.Data.GetData(typeof(SolidColorBrush));
            ((Label)sender).Content =  e.Data.GetData(DataFormats.Text);
            ((Label)sender).Background = scb1;

File APIs for .NET
Aspose are the market leader of .NET APIs for file business formats – natively work with DOCX, XLSX, PPT, PDF, MSG, MPP, images formats and many more!