Drag & drop without remove element.

12 posts, 1 answers
  1. Marcin
    Marcin avatar
    28 posts
    Member since:
    Jan 2009

    Posted 24 May 2010 Link to this post

    I have implemented drag&drop like in your example (RadTreeView -> ListBox). This is working but I want not remove element from RTV. Can you help me? Where is the mistake?
    Thanks.

    MainPage.xaml
    <UserControl 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"  
        xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  
        xmlns:dragDrop="clr-namespace:Telerik.Windows.Controls.DragDrop;assembly=Telerik.Windows.Controls" 
        xmlns:primitives="clr-namespace:Telerik.Windows.Controls.Primitives;assembly=Telerik.Windows.Controls" 
        xmlns:example="clr-namespace:InSEAAtr"  
        x:Class="InSEAAtr.MainPage" 
        mc:Ignorable="d" 
        d:DesignHeight="600" d:DesignWidth="800"
        <UserControl.Resources>        
     
            <LinearGradientBrush x:Key="DropPossibleBackground" StartPoint="0 0" EndPoint="0 1"
                <GradientStop Offset="0" Color="White" /> 
                <GradientStop Offset="1" Color="#FFE699" /> 
            </LinearGradientBrush> 
     
            <telerik:HierarchicalDataTemplate x:Key="CategoryTemplate" ItemsSource="{Binding Items}"
                <StackPanel Orientation="Horizontal"
                    <Image Width="16" Height="16" Source="Images/Icons/AtrValidate16.png" 
        Margin="3" VerticalAlignment="Center" /> 
                    <TextBlock Text="{Binding Title}" Margin="2" VerticalAlignment="Center" /> 
                </StackPanel> 
            </telerik:HierarchicalDataTemplate> 
     
            <DataTemplate x:Name="ProductTemplate"
                <telerik:RadDockPanel Margin="2" MaxWidth="200"
                    <TextBlock Text="{Binding Name}" FontWeight="Bold" telerik:RadDockPanel.Dock="Top" /> 
                    <TextBlock Text="{Binding UnitPrice}" Foreground="Green" telerik:RadDockPanel.Dock="Left" /> 
                    <TextBlock Text="{Binding Description}" telerik:RadDockPanel.Dock="Left" Margin="2 0 0 0" Foreground="Gray" TextWrapping="Wrap" /> 
                </telerik:RadDockPanel> 
            </DataTemplate> 
     
            <DataTemplate x:Name="MenuTemplate"
                <telerik:RadDockPanel Margin="2" MaxWidth="200"
                    <TextBlock Text="{Binding Name}" FontWeight="Bold" telerik:RadDockPanel.Dock="Top" /> 
                    <TextBlock Text="{Binding Count}" telerik:RadDockPanel.Dock="Left" /> 
                </telerik:RadDockPanel> 
            </DataTemplate> 
     
            <example:ExampleTemplateSelector x:Key="ExampleTemplateSelector" 
                      CategoryTemplate="{StaticResource CategoryTemplate}" 
                      ProductTemplate="{StaticResource ProductTemplate}" 
                      MenuTemplate="{StaticResource MenuTemplate}"/> 
     
        </UserControl.Resources> 
     
     
     
        <Grid x:Name="LayoutRoot" > 
            <Grid.RowDefinitions> 
                <RowDefinition Height="30"/> 
                <RowDefinition Height="*"/> 
            </Grid.RowDefinitions> 
             
            <Border Grid.Row="0"
                <Border.Background> 
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"
                        <GradientStop Color="#FF828282" Offset="0"/> 
                        <GradientStop Color="#FF585858" Offset="1"/> 
                        <GradientStop Color="#FF4D4D4D" Offset="0.41"/> 
                        <GradientStop Color="#FF232323" Offset="0.431"/> 
                        <GradientStop Color="#FF151515" Offset="0.452"/> 
                        <GradientStop Color="#FF1B1B1B" Offset="0.469"/> 
                    </LinearGradientBrush> 
                </Border.Background> 
            </Border> 
            <StackPanel HorizontalAlignment="Left" Margin="4,4,0,4" Orientation="Horizontal"  d:LayoutOverrides="Height"
                <telerik:RadButton x:Name="but3" Content="" Margin="0,0,5,0" Width="22"/> 
                <telerik:RadButton x:Name="but4" Content="" Margin="0,0,5,0" Width="22"/> 
                <telerik:RadButton x:Name="but1" Content="" Margin="0,0,5,0" Width="22"/> 
                <telerik:RadButton x:Name="but2" Content="" Margin="0,0,0,0" Width="22"/> 
            </StackPanel> 
     
            <StackPanel x:Name="spZalogowany" HorizontalAlignment="Center" Margin="0,7" Orientation="Horizontal"
                <sdk:Label x:Name="labLogowanie" d:LayoutOverrides="Height" Foreground="White" Content="Zalogowano: "/> 
                <sdk:Label x:Name="labUser" d:LayoutOverrides="Height" Foreground="White" Content="" ToolTipService.ToolTip="Zalogowany użytkownik"/> 
            </StackPanel> 
            <telerik:RadButton x:Name="butWyloguj" Content="Wyloguj" HorizontalAlignment="Right" Margin="0,4,8,4" d:LayoutOverrides="Height"/> 
     
            <Border Grid.Row="1" Height="30" VerticalAlignment="Top"
                <Border.Background> 
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"
                        <GradientStop Color="#FF828282" Offset="0"/> 
                        <GradientStop Color="#FF585858" Offset="1"/> 
                        <GradientStop Color="#FF4D4D4D" Offset="0.41"/> 
                        <GradientStop Color="#FF232323" Offset="0.431"/> 
                        <GradientStop Color="#FF151515" Offset="0.452"/> 
                        <GradientStop Color="#FF1B1B1B" Offset="0.469"/> 
                    </LinearGradientBrush> 
                </Border.Background> 
            </Border> 
     
            <telerik:RadDocking Margin="0,30,0,0" Grid.Row="1" d:LayoutOverrides="Width, Height"
                <telerik:RadDocking.DocumentHost> 
                    <telerik:RadSplitContainer> 
                        <telerik:RadPaneGroup> 
                            <telerik:RadPane x:Name="rpGrid" Header="Dokumenty" CanUserClose="False"
                                <telerik:RadGridView/> 
                            </telerik:RadPane> 
                        </telerik:RadPaneGroup> 
                    </telerik:RadSplitContainer> 
                </telerik:RadDocking.DocumentHost> 
                <telerik:RadSplitContainer> 
                    <telerik:RadPaneGroup SelectedIndex="0"
                        <telerik:RadPane x:Name="rpDrzewo" Header="Menu" CanUserClose="False"
                            <telerik:RadTreeView x:Name="rtvDokumenty" IsLineEnabled="True"  
                                                 IsDragDropEnabled="True" ItemTemplateSelector="{StaticResource ExampleTemplateSelector}"
                                <Style TargetType="telerik:RadTreeViewItem"
                                    <Setter Property="IsExpanded" Value="True" /> 
                                </Style> 
     
                            </telerik:RadTreeView> 
                        </telerik:RadPane> 
     
                        <telerik:RadPane x:Name="rpSkroty" Header="Moje dokumenty" CanUserClose="False"
                            <ListBox x:Name="lbSkroty" dragDrop:RadDragAndDropManager.AllowDrop="True"
                                <ListBox.ItemTemplate> 
                                    <DataTemplate> 
                                        <StackPanel Orientation="Vertical" HorizontalAlignment="Center" Width="100"
                                            
                                            <Image Source="{Binding Icon}" HorizontalAlignment="Center" Width="16" Height="16"/> 
                                            <TextBlock Text="{Binding Name}" FontWeight="Bold"  HorizontalAlignment="Center" 
                                                       TextWrapping="Wrap"/> 
                                            <TextBlock Text="{Binding Count}"  HorizontalAlignment="Center"  
                                                       TextWrapping="Wrap"/> 
                                        </StackPanel> 
                                    </DataTemplate> 
                                </ListBox.ItemTemplate> 
                                <ListBox.Style> 
                                    <Style TargetType="ListBox"
                                        <Setter Property="dragDrop:RadDragAndDropManager.AllowDrop" Value="True" /> 
                                    </Style> 
                                </ListBox.Style> 
                                <ListBox.ItemContainerStyle> 
                                    <Style TargetType="Control"
                                       
                                        <Setter Property="dragDrop:RadDragAndDropManager.AllowDrop" Value="True" /> 
                                    </Style> 
                                </ListBox.ItemContainerStyle> 
                                <ListBox.ItemsPanel> 
                                    <ItemsPanelTemplate> 
                                        <primitives:RadUniformGrid HorizontalAlignment="Center" VerticalAlignment="Top" Columns="1"/> 
                                    </ItemsPanelTemplate> 
                                </ListBox.ItemsPanel> 
                            </ListBox> 
                        </telerik:RadPane> 
     
                    </telerik:RadPaneGroup> 
                </telerik:RadSplitContainer> 
                <telerik:RadSplitContainer InitialPosition="DockedBottom"
                    <telerik:RadPaneGroup SelectedIndex="-1"
                        <telerik:RadPane Header="Pozycja obiegu" x:Name="rpObieg" CanUserClose="False"/> 
                    </telerik:RadPaneGroup> 
                </telerik:RadSplitContainer> 
            </telerik:RadDocking> 
     
     
        </Grid> 
    </UserControl> 
     

    MainPage.cs
    using System; 
    using System.Collections; 
    using System.Collections.Generic; 
    using System.Collections.ObjectModel; 
    using System.Linq; 
    using System.Windows; 
    using System.Windows.Controls; 
    using System.Windows.Input; 
    using System.Windows.Media; 
    using Telerik.Windows.Controls; 
    using Telerik.Windows.Controls.DragDrop; 
    using Telerik.Windows.Controls.TreeView; 
    using Telerik.Windows.Controls.Primitives; 
     
    namespace InSEAAtr 
        public partial class MainPage : UserControl 
        { 
            //ObservableCollection<RadTreeViewItem> allMenu = Generate(); 
            ObservableCollection<RadTreeViewItem> myMenu; 
     
            public MainPage() 
            { 
                InitializeComponent(); 
     
                rtvDokumenty.Items.Clear(); 
                rtvDokumenty.ItemsSource = CategoryViewModel.Generate(); 
     
     
     
                IList orderSource = new ObservableCollection<MenuViewModel>(); 
     
                lbSkroty.ItemsSource = orderSource; 
     
     
                RadDragAndDropManager.AddDropQueryHandler(lbSkroty, OnDropQuery); 
                RadDragAndDropManager.AddDropInfoHandler(lbSkroty, OnDropInfo); 
                RadDragAndDropManager.AddDragQueryHandler(lbSkroty, OnDragQuery); 
                RadDragAndDropManager.AddDragInfoHandler(lbSkroty, OnDragInfo); 
     
                //demo! 
     
     
     
            } 
             #region demo 
            // OnDragQuery event handler 
            private void OnDragQuery(object sender, DragDropQueryEventArgs e) 
            { 
                System.Windows.Controls.ListBox listBox = sender as System.Windows.Controls.ListBox; 
     
                if (listBox != null
                { 
                    IList selectedItems = listBox.SelectedItems.Cast<object>().ToList(); 
                    e.Options.Payload = selectedItems; 
                } 
                e.QueryResult = true
                e.Handled = true
            } 
            // OnDropQuery event handler 
            private void OnDropQuery(object sender, DragDropQueryEventArgs e) 
            { 
                ICollection draggedItems = e.Options.Payload as ICollection; 
                bool result = draggedItems.Cast<object>().All((object item) => item is MenuViewModel); 
                e.QueryResult = result; 
                e.Handled = true
            } 
            // OnDragInfo event handler 
            private void OnDragInfo(object sender, DragDropEventArgs e) 
            { 
                System.Windows.Controls.ListBox listBox = sender as System.Windows.Controls.ListBox; 
                IEnumerable draggedItems = e.Options.Payload as IEnumerable; 
                if (e.Options.Status == DragStatus.DragInProgress) 
                { 
                    //Set up a drag cue: 
                    TreeViewDragCue cue = new TreeViewDragCue(); 
                    cue.ItemTemplate = listBox.ItemTemplate; 
                    cue.ItemsSource = draggedItems; 
                    e.Options.DragCue = cue; 
                } 
                else if (e.Options.Status == DragStatus.DragComplete) 
                { 
                    IList source = listBox.ItemsSource as IList; 
                    foreach (object draggedItem in draggedItems) 
                    { 
                        //source.Remove(draggedItem); 
                    } 
                } 
            } 
            // OnDropInfo event handler 
            private void OnDropInfo(object sender, DragDropEventArgs e) 
            { 
                System.Windows.Controls.ItemsControl wishlist = e.Options.Destination as System.Windows.Controls.ItemsControl; 
                ICollection draggedItems = e.Options.Payload as ICollection; 
                if (wishlist == null
                    return
                // Get the drag cu that the TreeView or we have created 
                TreeViewDragCue cue = e.Options.DragCue as TreeViewDragCue; 
                if (e.Options.Status == DragStatus.DropPossible) 
                { 
                    // Set a suitable text: 
                    cue.DragActionContent = String.Format("Dodaj do Moich Dokumentów"); 
                    cue.IsDropPossible = true
                    wishlist.Background = this.Resources["DropPossibleBackground"as Brush; 
                } 
                else if (e.Options.Status == DragStatus.DropImpossible) 
                { 
                    cue.DragActionContent = null
                    cue.IsDropPossible = false
                } 
                else if (e.Options.Status == DragStatus.DropComplete) 
                { 
                    IList items = wishlist.ItemsSource as IList; 
                    foreach (object draggedItem in draggedItems) 
                    { 
                        items.Add(draggedItem); 
                    } 
     
     
                } 
                if (e.Options.Status != DragStatus.DropPossible) 
                { 
                    wishlist.Background = new SolidColorBrush(Colors.White); 
                } 
            } 
            #endregion 
     
        } 
     
     
     
        public class CategoryViewModel 
        { 
            public static IList Generate() 
            { 
                CategoryViewModel fak = new CategoryViewModel(); 
                fak.Title = "Faktury"
                foreach (MenuViewModel item in MenuViewModel.Generate(1)) 
                { 
                    fak.Items.Add(item); 
                } 
                CategoryViewModel kor = new CategoryViewModel(); 
                kor.Title = "Korespondencja"
                foreach (MenuViewModel item in MenuViewModel.Generate(2)) 
                { 
                    kor.Items.Add(item); 
                } 
                return new ObservableCollection<object>() 
                 { 
                  fak, kor 
                 }; 
            } 
            public CategoryViewModel() 
            { 
                Items = new ObservableCollection<object>(); 
            } 
            public string Title 
            { 
                get
                set
            } 
            public IList Items 
            { 
                get
                set
            } 
        } 
     
        public class ProductViewModel 
        { 
            // Data generation. 
            private static Random generator = new Random(1676545846); 
            private static string[] adjectives = "Fabulous,Amazing,New,Classic,Modern,Durable,Outstanding,Excellent,Premium".Split(','); 
            private static string[] owner = "Alaska,Jonhn,Ray,Ruby,Stone,Lilly,Scott,Barney,Dorian,Neo,Sarah".Split(','); 
            private static string[] objects = "Pen,Manual,Bicycle,Umbrella,Mouse,Vase,Keyboard".Split(','); 
            private static decimal[] prices = { 12.99M, 13.15M, 24.99M, 33.99M, 9.99M, 15.99M, 16.99M, 12.50M }; 
            public static IEnumerable<ProductViewModel> Generate(int count) 
            { 
                for (int i = 0; i < count; i++) 
                { 
                    string product = objects[generator.Next(objects.Length)]; 
                    ProductViewModel result = new ProductViewModel(); 
                    result.Name = String.Format("{0}'s {1} {2}"
                            owner[generator.Next(owner.Length)], 
                            adjectives[generator.Next(adjectives.Length)], 
                            product); 
                    result.UnitPrice = prices[generator.Next(prices.Length)]; 
                    result.Description = String.Format("Exquisite handcrafted {0}.", product.ToLower()); 
                    yield return result; 
                } 
            } 
            public string Name 
            { 
                get
                set
            } 
            public string Description 
            { 
                get
                set
            } 
            public decimal UnitPrice 
            { 
                get
                set
            } 
        } 
     
        public class MenuViewModel 
        { 
            public static List<MenuViewModel> Generate(int count) 
            { 
     
                List<MenuViewModel> result = new List<MenuViewModel>(); 
                 
                switch (count) 
                { 
                    case 1: 
                        MenuViewModel m2 = new MenuViewModel(); 
                        m2.Count = 2; 
                        m2.Name = "Korespondencja przychodząca"
                        m2.Icon = "Images/Icons/AtrValidate16.png"
                        result.Add(m2); 
     
                        MenuViewModel m3 = new MenuViewModel(); 
                        m3.Count = 2; 
                        m3.Name = "Korespondencja wychodząca"
                        m3.Icon = "Images/Icons/BUzytkownikUsun16.png"
                        result.Add(m3); 
     
                        break
                    case 2: 
     
     
                        MenuViewModel m6 = new MenuViewModel(); 
                        m6.Count = 6; 
                        m6.Name = "Faktury kosztowe"
                        m6.Icon = "Images/Icons/BUzytkownikEdytuj16.png"
                        result.Add(m6); 
     
                        MenuViewModel m1 = new MenuViewModel(); 
                        m1.Count = 6; 
                        m1.Name = "Faktury kosztowe"
                        m1.Icon = "Images/Icons/BUzytkownikHaslo16.png"
                        result.Add(m1); 
     
                        MenuViewModel m4 = new MenuViewModel(); 
                        m4.Count = 2; 
                        m4.Name = "Faktury drobne"
                        m4.Icon = "Images/Icons/BUzytkownikNieaktywni16.png"
                        result.Add(m4); 
                        break
                    default
                        break
     
                } 
                return result; 
            } 
     
            public string Name 
            { 
                get
                set
            } 
     
            public int Count 
            { 
                get
                set
            } 
     
            public string Icon 
            { 
                get
                set
            } 
        } 
     
        public class ExampleTemplateSelector : Telerik.Windows.Controls.DataTemplateSelector 
        { 
            public override DataTemplate SelectTemplate(object item, DependencyObject container) 
            { 
                if (item is ProductViewModel) 
                    return ProductTemplate; 
                else if (item is CategoryViewModel) 
                    return CategoryTemplate; 
                else if (item is MenuViewModel) 
                    return MenuTemplate; 
                return null
            } 
            public DataTemplate ProductTemplate 
            { 
                get
                set
            } 
            public DataTemplate CategoryTemplate 
            { 
                get
                set
            } 
     
            public DataTemplate MenuTemplate 
            { 
                get
                set
            } 
        } 
     
  2. Answer
    Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 26 May 2010 Link to this post

    Hello Marcin,

    You can handle the PreviewDragEnded event (set e.Handled  = true for its event args) and this will mean that the RadTreeView will not modify its collections.

    private void treeView_PreviewDragEnded(object sender, RadTreeViewDragEndedEventArgs e)
    {
        e.Handled = true;
    }

    Give this a try and let me know if it works for you or if you need more info.

    Kind regards,
    Tina Stancheva
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. DevCraft banner
  4. Marcin
    Marcin avatar
    28 posts
    Member since:
    Jan 2009

    Posted 26 May 2010 Link to this post

    Thank you, Tina

    Kind regards,
    Marcin
  5. Hans
    Hans avatar
    28 posts
    Member since:
    Mar 2011

    Posted 17 Mar 2011 Link to this post

    I have the same problem, But I want my dragged TreeViewItem to move when dropped inside the treeview.
    But when I drag it into a RichTextBox, I just want to know what kind of item is dropped into the richTextBox.
    And the item inside the TreeView to stay in place.

    With this solution its either don't do anything at all or always get moved.
    And in the RadTreeViewDragEndedEventArgs I can't find anything that refers to the RichTextBox.
    So I can't tell where its dropped within this event.
  6. Tsvyatko
    Admin
    Tsvyatko avatar
    833 posts

    Posted 22 Mar 2011 Link to this post

    Hello Hans,

     You might want to modify this code as follows:

    private void RadTreeView_PreviewDragEnded(object sender, Telerik.Windows.Controls.RadTreeViewDragEndedEventArgs e)
    {
        e.Handled = RadDragAndDropManager.Options.Destination != sender;
    }


    Regards,
    Tsvyatko
    the Telerik team
  7. loowool
    loowool avatar
    60 posts
    Member since:
    Sep 2006

    Posted 08 Apr 2011 Link to this post

    Dear telerik,

    I have a similar situation, but I followed the steps of the demo found in ScheduleView.SilverLight/DragDrop from a ListBox to a ScheduleView. I'd like to Clone the Item in the ListView to the ScheduleView NOT REMOVE IT. How do I do that?

    Thanks as always.
    Salah A. Malaeb
  8. Valeri Hristov
    Admin
    Valeri Hristov avatar
    2252 posts

    Posted 12 Apr 2011 Link to this post

    Hi,

    Unfortunately this behavior cannot be easily changed, if possible at all. It appears that RadScheduleView automatically removes the dragged items from the source and we do not provide API to override this. We are working on such changes, but they will not be included in the upcoming SP1 this week. Currently you need to hold Ctrl while releasing the mouse button to copy the appointments.

    All the best,
    Valeri Hristov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  9. Tarcisis
    Tarcisis avatar
    29 posts
    Member since:
    Nov 2010

    Posted 28 Jul 2011 Link to this post

    Are you able to open the dialog (appointmentEdit) when you drop the element into ScheduleView?
  10. Andrew
    Andrew avatar
    4 posts
    Member since:
    Oct 2012

    Posted 07 Jul 2015 Link to this post

    How can I disable the 'remove dragged item from drag source' with the DragDropManager? The PreviewDragEnded event is deprecated and appears not to be hit. Thanks.
  11. Kalin
    Admin
    Kalin avatar
    1208 posts

    Posted 08 Jul 2015 Link to this post

    Hi Andrew,

    Can you please share some more details on the exact controls you are using and how the drag drop functionality is implemented (some sample code or project would be helpful)? 

    I'm looking forward to hearing from you.

    Regards,
    Kalin
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  12. Andrew
    Andrew avatar
    4 posts
    Member since:
    Oct 2012

    Posted 08 Jul 2015 in reply to Kalin Link to this post

    Thanks for the reply. I managed to find my error, I was using a mix of the old and new Drag Drop methodologies - moving fully to the DragDropManager solved the issue. Thanks again.
  13. Kalin
    Admin
    Kalin avatar
    1208 posts

    Posted 09 Jul 2015 Link to this post

    Hi Andrew,

    I'm glad you have managed to resolve the issue. If you have any other questions or concerns, please let us know.

    Regards,
    Kalin
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top
DevCraft banner