dropinfo event is not fired

9 posts, 1 answers
  1. Seo
    Seo avatar
    16 posts
    Member since:
    Sep 2010

    Posted 30 Aug 2011 Link to this post

    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.
  2. Answer
    Maya
    Admin
    Maya avatar
    4062 posts

    Posted 03 Sep 2011 Link to this post

    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 >>

  3. DevCraft banner
  4. Seo
    Seo avatar
    16 posts
    Member since:
    Sep 2010

    Posted 05 Sep 2011 Link to this post

    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.
  5. Seo
    Seo avatar
    16 posts
    Member since:
    Sep 2010

    Posted 20 Sep 2011 Link to this post

    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
  6. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 21 Sep 2011 Link to this post

    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 >>

  7. Seo
    Seo avatar
    16 posts
    Member since:
    Sep 2010

    Posted 25 Sep 2011 Link to this post

    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
  8. Tsvyatko
    Admin
    Tsvyatko avatar
    833 posts

    Posted 27 Sep 2011 Link to this post

    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 >>

  9. Seo
    Seo avatar
    16 posts
    Member since:
    Sep 2010

    Posted 27 Sep 2011 Link to this post

    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
  10. Sreevalli
    Sreevalli avatar
    7 posts
    Member since:
    Sep 2012

    Posted 27 Feb 2013 Link to this post

    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

Back to Top
DevCraft banner