DragDrop.Behaviors cause to duplicate data

1 posts, 0 answers
  1. manoj savalia
    manoj savalia avatar
    11 posts
    Member since:
    Jul 2009

    Posted 22 Feb 2012 Link to this post

    I have two list box. in my list box drag and drop functionality work fine. But it is not work when rearrange order of any listbox and then drag and drop.
    I also add my code ....


    I have one file DragVisualProvider.cs which is like



    using System;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using Telerik.Windows.DragDrop.Behaviors;
    using System;
    using System.Net;
    using System.Windows;
    using Telerik.Windows.DragDrop.Behaviors;
    using Telerik.Windows.DragDrop;
    using Telerik.Windows.Controls;
    using System.Linq;

    namespace DragCuePictureTest
    {
        public class DragVisualProvider : DependencyObject, IDragVisualProvider
        {
            public DataTemplate DraggedItemTemplate
            {
                get
                {
                    return (DataTemplate)GetValue(DraggedItemTemplateProperty);
                }
                set
                {
                    SetValue(DraggedItemTemplateProperty, value);
                }
            }

            public static readonly DependencyProperty DraggedItemTemplateProperty =
            DependencyProperty.Register("DraggedItemTemplate", typeof(DataTemplate), typeof(DragVisualProvider), new PropertyMetadata(null));

            public FrameworkElement CreateDragVisual(DragVisualProviderState state)
            {
                var visual = new DragVisual();

                var theme = StyleManager.GetTheme(state.Host);
                if (theme != null)
                {
                    StyleManager.SetTheme(visual, theme);
                }

                visual.Content = state.DraggedItems.OfType<object>().FirstOrDefault();
                visual.ContentTemplate = this.DraggedItemTemplate;

                return visual;
            }

            public Point GetDragVisualOffset(DragVisualProviderState state)
            {
                return state.RelativeStartPoint;
            }

            public bool UseDefaultCursors { get; set; }


        }
    }



    In my Example.xaml File



    <UserControl x:Class="DragCuePictureTest.Example"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
                 xmlns:dragDropBehaviors="clr-namespace:Telerik.Windows.DragDrop.Behaviors;assembly=Telerik.Windows.Controls"
                 xmlns:telerikQuickStart="clr-namespace:Telerik.Windows.Controls.QuickStart;assembly=Telerik.Windows.Controls"
                 xmlns:local="clr-namespace:DragCuePictureTest"
                
                 VerticalAlignment="Center" HorizontalAlignment="Center">

        <telerikQuickStart:QuickStart.ExampleHeader>
            <TextBlock Text="Drag and Drop" Margin="8"  />
        </telerikQuickStart:QuickStart.ExampleHeader>

        <UserControl.Resources>
            <!--<local:MainViewModel x:Key="ViewModel"/>-->
            <telerik:MetroColors x:Key="MetroColors" />
            <SolidColorBrush x:Key="AccentBrush" Color="{Binding Source={StaticResource MetroColors}, Path=Palette.AccentColor}" />
            <SolidColorBrush x:Key="BasicBrush" Color="{Binding Source={StaticResource MetroColors}, Path=Palette.BasicColor}" />
            <SolidColorBrush x:Key="StrongBrush" Color="{Binding Source={StaticResource MetroColors}, Path=Palette.StrongColor}" />
            <SolidColorBrush x:Key="MainBrush" Color="{Binding Source={StaticResource MetroColors}, Path=Palette.MainColor}" />
            <SolidColorBrush x:Key="MarkerBrush" Color="{Binding Source={StaticResource MetroColors}, Path=Palette.MarkerColor}" />
            <SolidColorBrush x:Key="ValidationBrush" Color="{Binding Source={StaticResource MetroColors}, Path=Palette.ValidationColor}" />
            <DataTemplate x:Key="ApplicationDragTemplate">
                <!--<Image Source="{Binding IconPath}" Stretch="None" VerticalAlignment="Top" />-->
                <TextBlock Text="{Binding Name}" />
            </DataTemplate>
            <Style TargetType="ListBoxItem" x:Key="draggableItemStyle">
                <Setter Property="HorizontalAlignment" Value="Stretch" />
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                <Setter Property="Foreground" Value="#000000" />
                <Setter Property="telerik:DragDropManager.AllowDrag" Value="True" />
                <Setter Property="telerik:DragDropManager.AllowCapturedDrag" Value="True" />
            </Style>

        </UserControl.Resources>
        <Grid x:Name="LayoutRoot" Width="700" Height="450">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <ListBox x:Name="allApplicationsBox" BorderThickness="0" telerik:StyleManager.Theme="Metro" Margin="0,0,3,0"
                     ItemsSource="{Binding AllApplications}"
                     AllowDrop="True" Background="Transparent" BorderBrush="Transparent"
                     ItemContainerStyle="{StaticResource draggableItemStyle}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center" Background="LightCyan">
                            <Image Source="{Binding IconPath}"
                                   HorizontalAlignment="Center" />
                            <TextBlock Text="{Binding Name}" Foreground="{StaticResource StrongBrush}"
                                       HorizontalAlignment="Center" />
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>

                <dragDropBehaviors:ListBoxDragDrop.Behavior>
                    <dragDropBehaviors:ListBoxDragDropBehavior />
                </dragDropBehaviors:ListBoxDragDrop.Behavior>
                <dragDropBehaviors:ListBoxDragDrop.DragVisualProvider>
                    <local:DragVisualProvider DraggedItemTemplate="{StaticResource ApplicationDragTemplate}"/>
                </dragDropBehaviors:ListBoxDragDrop.DragVisualProvider>
            </ListBox>
            <ListBox x:Name="myApplicationsBox" Background="Transparent" telerik:StyleManager.Theme="Metro" Grid.Column="1"
                    
                     AllowDrop="True" BorderThickness="0" BorderBrush="Transparent"
                     ItemContainerStyle="{StaticResource draggableItemStyle}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Vertical">
                            <Grid Width="100" Height="100" Background="Purple">
                                <Grid.RowDefinitions>
                                    <RowDefinition />
                                    <RowDefinition />
                                    <RowDefinition />
                                </Grid.RowDefinitions>
                                <Image Grid.Row="0" HorizontalAlignment="Center"
                                   Source="{Binding IconPath}" Width="32" Height="32"
                                   Margin="0 0 5 0" />
                                <TextBlock Grid.Row="1" Text="{Binding Name}" FontWeight="Bold"
                                       HorizontalAlignment="Center" Foreground="{StaticResource StrongBrush}"/>
                                <TextBlock Text="{Binding Author}" Grid.Row="2"
                                       HorizontalAlignment="Center" Foreground="{StaticResource StrongBrush}"/>
                            </Grid>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <telerik:RadUniformGrid Columns="1" HorizontalAlignment="Left" VerticalAlignment="Top" />
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
                <dragDropBehaviors:ListBoxDragDrop.Behavior>
                    <dragDropBehaviors:ListBoxDragDropBehavior />
                </dragDropBehaviors:ListBoxDragDrop.Behavior>
                <dragDropBehaviors:ListBoxDragDrop.DragVisualProvider>
                    <local:DragVisualProvider DraggedItemTemplate="{StaticResource ApplicationDragTemplate}"/>
                </dragDropBehaviors:ListBoxDragDrop.DragVisualProvider>
            </ListBox>
        </Grid>

    </UserControl>


    In my Example.xaml.cs file


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.Collections.ObjectModel;

    namespace DragCuePictureTest
    {
        public partial class Example : UserControl
        {
            public Example()
            {
                InitializeComponent();
                myApplicationsBox.ItemsSource = MyApplications;
                allApplicationsBox.ItemsSource = AllApplications;
            }


            private ObservableCollection<ApplicationInfo> allApplications = GenerateApplicationInfos();
            private ObservableCollection<ApplicationInfo> myApplications = GenerateMyApplicationInfos();

            public ObservableCollection<ApplicationInfo> AllApplications
            {
                get
                {
                    return this.allApplications;
                }
                set
                {
                    this.allApplications = value;
                }
            }

            public ObservableCollection<ApplicationInfo> MyApplications
            {
                get
                {
                    return this.myApplications;
                }
                set
                {
                    this.myApplications = value;
                }
            }

            public static ObservableCollection<ApplicationInfo> GenerateApplicationInfos()
            {
                ObservableCollection<ApplicationInfo> result = new ObservableCollection<ApplicationInfo>();

                ApplicationInfo info1 = new ApplicationInfo();
                info1.Name = "Large Collider";
                info1.Author = "C.E.R.N.";
                info1.IconPath = @"../Images/DragAndDrop/LargeIcons/Atom.png";
                result.Add(info1);

                ApplicationInfo info2 = new ApplicationInfo();
                info2.Name = "Paintbrush";
                info2.Author = "Imagine Inc.";
                info2.IconPath = @"../Images/DragAndDrop/LargeIcons/Brush.png";
                result.Add(info2);

                ApplicationInfo info3 = new ApplicationInfo();
                info3.Name = "Lively Calendar";
                info3.Author = "Control AG";
                info3.IconPath = @"../Images/DragAndDrop/LargeIcons/CalendarEvents.png";
                result.Add(info3);

                ApplicationInfo info4 = new ApplicationInfo();
                info4.Name = "Fire Burning ROM";
                info4.Author = "The CD Factory";
                info4.IconPath = @"../Images/DragAndDrop/LargeIcons/CDBurn.png";
                result.Add(info4);

                ApplicationInfo info5 = new ApplicationInfo();
                info5.Name = "Fav Explorer";
                info5.Author = "Star Factory";
                info5.IconPath = @"../Images/DragAndDrop/LargeIcons/favorites.png";
                result.Add(info5);

                ApplicationInfo info6 = new ApplicationInfo();
                info6.Name = "IE Fox";
                info6.Author = "Open Org";
                info6.IconPath = @"../Images/DragAndDrop/LargeIcons/Connected.png";
                result.Add(info6);

                return result;
            }

            public static ObservableCollection<ApplicationInfo> GenerateMyApplicationInfos()
            {
                ObservableCollection<ApplicationInfo> result = new ObservableCollection<ApplicationInfo>();

                ApplicationInfo info7 = new ApplicationInfo();
                info7.Name = "Charting";
                info7.Author = "AA-AZ inc";
                info7.IconPath = @"../Images/DragAndDrop/LargeIcons/ChartDot.png";
                result.Add(info7);

                ApplicationInfo info8 = new ApplicationInfo();
                info8.Name = "SuperPlay";
                info8.Author = "EB Games";
                info8.IconPath = @"../Images/DragAndDrop/LargeIcons/Games.png";
                result.Add(info8);

                return result;
            }
        }

        public class ApplicationInfo
        {
            public Double Price
            {
                get;
                set;
            }

            public String IconPath
            {
                get;
                set;
            }

            public String Name
            {
                get;
                set;
            }

            public String Author
            {
                get;
                set;
            }
        }
    }



    Please reply as soon as.....


Back to Top