This is a migrated thread and some comments may be shown as answers.

dropinfo event is not fired

8 Answers 112 Views
DragAndDrop
This is a migrated thread and some comments may be shown as answers.
Seo
Top achievements
Rank 1
Seo asked on 30 Aug 2011, 11:44 AM
I have implemented the multiple selection on drag and drop referring to your help site.
But there is something odd.
Items can be selected and dragged in multiple but they are not dropped.
The dropinfo event is not fired ever. And I don't know where the problem is in my code.
Here is my code.

<XAML>
<UserControl x:Class="SilverlightApplication1.MainPage"
    xmlns:local="clr-namespace:SilverlightApplication1"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
 
    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.Resources>
            <Style TargetType="ListBoxItem">
                <Setter Property="telerik:RadDragAndDropManager.AllowDrop" Value="True"/>
                <Setter Property="telerik:RadDragAndDropManager.AllowDrag" Value="True"/>
            </Style>
            <Style TargetType="local:ExtendedListBoxItem">
                <Setter Property="telerik:RadDragAndDropManager.AllowDrag" Value="True"/>
                <Setter Property="telerik:RadDragAndDropManager.AutoDrag" Value="True"/>
            </Style>
        </Grid.Resources>
 
        <local:ExtendedListBox x:Name="SourceBox" ItemsSource="{Binding Data}"
                          SelectionMode="Extended"
                          ClickOnSelectedItemAction="KeepSelectionIfItemSelected">
            <local:ExtendedListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding UserName}"/>
                </DataTemplate>
            </local:ExtendedListBox.ItemTemplate>
        </local:ExtendedListBox>
 
        <!--
        <local:ExtendedListBox x:Name="DestinationBox" ItemsSource="{Binding Data}" SelectionMode="Extended" Grid.Row="1">
            <local:ExtendedListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding UserName}"/>
                </DataTemplate>
            </local:ExtendedListBox.ItemTemplate>
        </local:ExtendedListBox>
        -->
         
 
        <ListBox x:Name="DestinationBox" ItemsSource="{Binding Data}" SelectionMode="Extended" Grid.Row="1">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding UserName}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
 
    </Grid>
</UserControl>

<C#>
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Telerik.Windows.Controls.DragDrop;
using Telerik.Windows.Controls.GridView;
using System.Collections;
using Telerik.Windows.Controls;
using System.Collections.ObjectModel;
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Windows.Media;
 
namespace SilverlightApplication1
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
 
            RadDragAndDropManager.ExecutionMode = DragExecutionMode.Legacy;
            RadDragAndDropManager.Initialize();
 
            //this.DataContext = new MainViewModel();
            //DestinationBox.DataContext = new MainViewModel();
 
            SourceBox.ItemsSource = Data.GetItems();
            //DestinationBox.ItemsSource = Data.GetItems();
 
            RadDragAndDropManager.AddDragInfoHandler(this.SourceBox, SourceBox_DragInfo);
            RadDragAndDropManager.AddDropInfoHandler(this.DestinationBox, DestinationBox_DropInfo);
        }
 
        private void SourceBox_DragInfo(object sender, Telerik.Windows.Controls.DragDrop.DragDropEventArgs e)
        {
            if (!(e.Options.DragCue is TreeListViewDragCue))
            {
                var draggedItems = ((System.Windows.Controls.ListBox)sender).SelectedItems.OfType<object>().ToList();
                var dragCue = new TreeListViewDragCue() { ItemsSource = draggedItems };
                dragCue.DisplayMemberPath = "UserName";
                dragCue.DragTooltipVisibility = Visibility.Collapsed;
                e.Options.DragCue = dragCue;
                e.Options.Payload = draggedItems;
            }
            if (e.Options.Status == DragStatus.DragComplete && e.Options.Payload is IList)
            {
                var itemsSource = ((System.Windows.Controls.ListBox)sender).ItemsSource as IList;
                foreach (var item in ((IList)e.Options.Payload))
                {
                    itemsSource.Remove(item);
                }
            }
        }
        private void DestinationBox_DropInfo(object sender, Telerik.Windows.Controls.DragDrop.DragDropEventArgs e)
        {
            if (e.Options.Status == DragStatus.DropComplete && e.Options.Payload is IList)
            {
                var itemsSource = ((System.Windows.Controls.ListBox)sender).ItemsSource as IList;
                foreach (var item in ((IList)e.Options.Payload))
                {
                    itemsSource.Add(item);
                }
            }
        }
    }
 
    public class ExtendedListBox : System.Windows.Controls.ListBox
    {
        public ClickAction ClickOnSelectedItemAction
        {
            get { return (ClickAction)GetValue(ClickOnSelectedItemActionProperty); }
            set { SetValue(ClickOnSelectedItemActionProperty, value); }
        }
        // Using a DependencyProperty as the backing store for ClickOnSelectedItemAction.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ClickOnSelectedItemActionProperty =
            DependencyProperty.Register("ClickOnSelectedItemAction", typeof(ClickAction), typeof(ExtendedListBox), new PropertyMetadata(ClickAction.OverrideSelection));
 
        protected override DependencyObject GetContainerForItemOverride()
        {
            //return base.GetContainerForItemOverride();
            ExtendedListBoxItem item = new ExtendedListBoxItem();
            item.ParentListBox = this;
            if (this.ItemContainerStyle != null)
            {
                item.Style = this.ItemContainerStyle;
            }
            return item;
        }
    }
 
    public enum ClickAction
    {
        KeepSelectionIfItemSelected,
        OverrideSelection
    }
 
    public class ExtendedListBoxItem : System.Windows.Controls.ListBoxItem
    {
        public ExtendedListBox ParentListBox { get; set; }
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            if (this.ParentListBox != null &&
                this.ParentListBox.ClickOnSelectedItemAction == ClickAction.KeepSelectionIfItemSelected &&
                this.IsSelected)
            {
                return;
            }
            base.OnMouseLeftButtonDown(e);
        }
    }
 
    public class Data
    {
        private String objName;
 
        public String UserName
        {
            get
            {
                return this.objName;
            }
            set
            {
                if (this.UserName == value)
                    return;
                this.objName = value;
                OnPropertyChanged("UserName");
            }
        }
 
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string info)
        {
            PropertyChangedEventHandler temp = this.PropertyChanged;
            if (temp != null)
                temp(this, new PropertyChangedEventArgs(info));
        }
 
        public static IList<object> GetItems()
        {
            ObservableCollection<object> result = new ObservableCollection<object>();
 
            for (int i = 0; i < 10; i++)
            {
                result.Add(new Data
                {
                    UserName = "UserName : " + i
                });
            }
 
            return result;
        }
    }
}

Thanks in advance.

8 Answers, 1 is accepted

Sort by
0
Accepted
Maya
Telerik team
answered on 03 Sep 2011, 08:25 AM
Hello Seo,

I am sending you a sample project illustrating the implementation of the example form dragging and dropping multiple items. Please take a look at it and let me know in case of any misunderstandings.


Kind regards,
Maya
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

0
Seo
Top achievements
Rank 1
answered on 06 Sep 2011, 02:57 AM
Hello Maya

Thank you for reply and sorry for the late reply.

Your sample project worked perfectly.
And I thought that the problem in my code was probably in the class generating data for list boxes.

Now I can move the items between list boxes and this is what I wanna do.

Thank you so much.
0
Seo
Top achievements
Rank 1
answered on 20 Sep 2011, 11:06 AM
Hello Maya

In the sample, each list box has items.
There is no problem with drag and drop on list boxes with items.
However in case the source list box has items and the destination list box doesn't,
the items of source list box are dragged, but they are not dropped in the destination list box.
Could you try this and if it was, notice the resolution to me?


Thank you
0
Maya
Telerik team
answered on 21 Sep 2011, 09:21 AM
Hello Seo,

That would be the expected behavior since AllowDrag and AllowDrop properties are set for the ListBoxItem/ExtendedListBoxItem. What you may try is to set AllowDrop directly to the DestinationBox for example:

<ListBox x:Name="DestinationBox"
               telerik:RadDragAndDropManager.AllowDrop="True"
               ItemsSource="{Binding Data1}"
               SelectionMode="Extended" Grid.Row="1" >
          <ListBox.ItemTemplate>
              <DataTemplate>
                  <TextBlock Text="{Binding UserName}"/>
              </DataTemplate>
          </ListBox.ItemTemplate>
      </ListBox>
 

Kind regards,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Seo
Top achievements
Rank 1
answered on 26 Sep 2011, 03:03 AM
Hello Maya

Thank you for your reply.

I added the line as mentioned above.
However while dropping items, nullpoint exception has occurred on the following method.

private void DestinationBox_DropInfo(object sender, Telerik.Windows.Controls.DragDrop.DragDropEventArgs e)
{
    if (e.Options.Status == DragStatus.DropComplete && e.Options.Payload is IList)
    {
        var itemsSource = ((System.Windows.Controls.ListBox)sender).ItemsSource as IList;
        foreach (var item in ((IList)e.Options.Payload))
        {
            itemsSource.Add(item);
        }
    }
}

The itemSource defined above is null.
It is supposed that the exception occurred when a list box has no items.

Thanks
0
Tsvyatko
Telerik team
answered on 27 Sep 2011, 08:25 AM
Hello Seo,

 In order this code to work the destination listbox's itemssource needs to be set to empty collection (not null), so that on drop the items will be inserted within it. If the listbox is bound to collection that is not IList then the itemsSource should be cast to the appropriate type.

Please have a look at the updated project and let us know if you have any further questions.

Regards,
Tsvyatko
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Seo
Top achievements
Rank 1
answered on 27 Sep 2011, 10:16 AM
Hello Tsvyatko

Thank you for you reply

The itemssource of the list box with no items is null definitely.
So I intentionally bound the list box to a collection that count is zero.
And this worked perfectly.

Thank you again
0
Sreevalli
Top achievements
Rank 1
answered on 27 Feb 2013, 10:08 AM
Hi,

I wanted to drop the image from my file directories. for this i'm using only destination and raising DropQuery &  DropInfo events. but it's not firing at all. is this possible to do? please provide me a sample application.

Thanks
Sreevalli

Tags
DragAndDrop
Asked by
Seo
Top achievements
Rank 1
Answers by
Maya
Telerik team
Seo
Top achievements
Rank 1
Tsvyatko
Telerik team
Sreevalli
Top achievements
Rank 1
Share this question
or