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

DragDropManager.AddDropHandler not working for ItemsControl

0 Answers 97 Views
DragAndDrop
This is a migrated thread and some comments may be shown as answers.
Yesim
Top achievements
Rank 1
Yesim asked on 24 Jan 2020, 11:25 AM

Hi,

DragDropManager.AddDropHandler not working for ItemsControl,

Here is my code. I also attached a sample project.

VIEW

<Window x:Class="DragDropExample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
        mc:Ignorable="d"
        Title="MainWindow"
        Height="500"
        Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <StackPanel Orientation="Vertical"
                    Margin="10">
            <TextBlock Text="SOURCE"
                       FontWeight="Bold" />

            <ItemsControl Name="Source"
                          VerticalAlignment="Stretch"
                          Height="400"
                          BorderBrush="Red"
                          BorderThickness="1">
                
                <Button  Content="DRAG ME"
                         Margin="10"
                         Width="100"
                         telerik:DragDropManager.AllowCapturedDrag="True" />

                <Button  Content="DRAG ME 1"
                         Margin="10"
                         Width="100"
                         telerik:DragDropManager.AllowCapturedDrag="True" />
            </ItemsControl>
        </StackPanel>

        <StackPanel  Grid.Column="1"
                     Margin="10"
                     Orientation="Vertical">
            <TextBlock Text="This is ItemsControl. NOT WORKING"
                       FontWeight="Bold" />
            <ItemsControl    Name="TargetItemsControl"
                             AllowDrop="True"
                             Height="400"
                             BorderBrush="Blue"
                             BorderThickness="1"
                             VerticalAlignment="Stretch" />
        </StackPanel>

        <StackPanel Grid.Column="2"
                    Margin="10"
                    Orientation="Vertical">
            <TextBlock Text="This is LlistBox. WORKING"
                       FontWeight="Bold" />

            <ListBox    Name="TargetListBox"
                        AllowDrop="True"
                        Height="400"
                        VerticalAlignment="Stretch"
                        BorderBrush="Blue"
                        BorderThickness="1"
                        Grid.Column="2" />
        </StackPanel>

    </Grid>
</Window>

 

CODE

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Telerik.Windows.DragDrop;

namespace DragDropExample
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DragDropManager.AddDragInitializeHandler(Source, OnDragInitialize);
            DragDropManager.AddGiveFeedbackHandler(Source, OnGiveFeedback);
            DragDropManager.AddDropHandler(TargetItemsControl, OnDrop);
            DragDropManager.AddDropHandler(TargetListBox, OnDrop);
        }
        private void OnDragInitialize(object sender, DragInitializeEventArgs args)
        {
            FrameworkElement source = sender as FrameworkElement;

            args.AllowedEffects = DragDropEffects.All;
            var payload = DragDropPayloadManager.GeneratePayload(null);
            var data = ((Button)args.OriginalSource).Content;
            payload.SetData("DragData", data);
            args.Data = payload;
            args.DragVisual = new ContentControl { Content = data };
        }

        private void OnGiveFeedback(object sender, Telerik.Windows.DragDrop.GiveFeedbackEventArgs args)
        {
            args.SetCursor(Cursors.Arrow);
            args.Handled = true;
        }

        private void OnDrop(object sender, Telerik.Windows.DragDrop.DragEventArgs e)
        {
            var data = ((DataObject)e.Data).GetData("DragData") as string;

            if (sender.GetType() == typeof(ListBox))
            {
                TargetListBox.Items.Add(data);
            }
            else if (sender.GetType() == typeof(ItemsControl))
            {
                TargetItemsControl.Items.Add(data);
            }
        }
    }
}



No answers yet. Maybe you can help?

Tags
DragAndDrop
Asked by
Yesim
Top achievements
Rank 1
Share this question
or