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
85
Drag and Drop funcationality in XamCarouselListBox
posted

I have a XamCarouselListBox which displays images. I want to drag and drop these images in other part of the grid.

Where should I create the PreviewMouseLeftButton down, up and mousemove events? On image or on XamCarouselListBox or on Grid.

Can someone please provide some sample code snippet or application?

  • 600
    Suggested Answer
    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]

  • 69686
    posted

    Hello,

    You can take a look at this blog post - http://blogs.infragistics.com/blogs/alex_fidanov/archive/2009/07/28/drag-amp-drop-with-datapresenter-family-controls.aspx.

     

    It shows how to implement drag and drop from a XamDataCarousel, but the approach is practically identical.