Forum guidelines
C# Drag and Drop - DragDrop.DoDragDrop()
Posted By C B on 09 Mar 2012

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;

Its only bloody obvious when you know how!
C# Corner Hyderabad Developer Conference


Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.