Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
695
Drag and Drop functionality
posted

Does this control support drag and drop functionality and if so could you povide me with some sample code?

  • 600
    posted

    Hi,

    This is what I did:

    Item Templete:

    <igWindows:XamCarouselListBox.ItemTemplate>
     <DataTemplate DataType="Imagen">
      <Grid Width="150" Height="150" Margin="0,0,0,0" >
       <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
       </Grid.RowDefinitions>
       <Image Source="{Binding RutaCompleta, Converter={StaticResource imageUriConverter}}"
                       Tag="{Binding IdImagen, Mode=OneWay}"
                       HorizontalAlignment="Left" Width="150" Height="150" VerticalAlignment="Top"
                       MouseLeftButtonDown="DragImage" />
      </Grid>
     </DataTemplate>
    </igWindows:XamCarouselListBox.ItemTemplate>

    Code:

            private void DragImage(object sender, MouseButtonEventArgs e)
            {
                try
                {
                    Image image = e.Source as Image;
                    if (e.ClickCount == 2)
                    {
                        this.SelectedImage.Source = image.Source;
                        ActualizarTexto();
                    }
                    else
                    {
                        imagen = GetDataFromListBox(e.GetPosition(ImagenesListBox)) as Imagen;
                        if (imagen != null)
                        {
                            this.ImagenesListBox.SelectedItem = imagen;
                        }
                        DataObject data = new DataObject(typeof(ImageSource), image.Source);
                        DragDrop.DoDragDrop(image, data, DragDropEffects.Copy);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());

                }
            }
            private void DropImage(object sender, DragEventArgs e)
            {
                try
                {
                    ImageSource image = e.Data.GetData(typeof(ImageSource)) as ImageSource;
                    if (image != null)
                    {
                        this.SelectedImage.Source = image;
                        ActualizarTexto();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
            private object GetDataFromListBox(Point point)
            {
                UIElement element = ImagenesListBox.InputHitTest(point) as UIElement;
                object data = DependencyProperty.UnsetValue;
                while (data == DependencyProperty.UnsetValue && element != null)
                {
                    data = ImagenesListBox.ItemContainerGenerator.ItemFromContainer(element);
                    if (data == DependencyProperty.UnsetValue)
                        element = VisualTreeHelper.GetParent(element) as UIElement;

                    ContentPresenter content = element as ContentPresenter;
                    if (content != null)
                    {
                        data = content.Content;
                        break;
                    }
                }
                if (data != DependencyProperty.UnsetValue)
                    return data;

                return null;
            }

     

    I hope this example will help you.

    Jaimir G. [MVP]