Forums - C# Corner

Forum guidelines
C B

C B

  • 1.3k
  • 22
  • 15.3k

C# Drag and Drop - DragDrop.DoDragDrop()

Mar 9 2012 12:12 PM
Hello

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.

Cheers

C


//C# Drag and Drop code

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        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;
        }
    }
}

VSLive! Anaheim

SPONSORED BY

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!