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

Drag and drop to empty GridView

4 Answers 137 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Espen
Top achievements
Rank 1
Espen asked on 01 Aug 2011, 03:50 PM
Ok...so I'm currently trying to drag and drop between 2 grids. 1 has all rows, the other one is empty. Trying to recreate all the different samples around...but I can't seem to enable the empty grid to accept drops. Is there any simple solution around where I can have a look? Setting

RadDragAndDropManager.AllowDrop="True"
on the grid doesn't seem to help

4 Answers, 1 is accepted

Sort by
0
Ivan Ivanov
Telerik team
answered on 02 Aug 2011, 08:11 AM
Hello Espen Berglund,

I have prepared a sample project for you. Please, refer to it and let me know whether this solution meets your requirements.

Regards,
Ivan Ivanov
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>
0
Espen
Top achievements
Rank 1
answered on 02 Aug 2011, 10:34 AM
That works...but when I implement it, I'm experiencing some really weird behavior. Basically the first drag and drop I do works. The second though works too, but if I drop it over the source grid, this grid magically has been able to accept drops and the row just disappears. Note that it doesn't happen on the first drag and drop, only the ones you do after that. Now, the code is basically the same you posted, although I've implemented it in behaviors.
public class AllowDragBehavior : Behavior<RadGridView>
{
protected override void OnAttached()
{
base.OnAttached();
RadDragAndDropManager.AddDragQueryHandler(AssociatedObject, OnDragQuery);
RadDragAndDropManager.AddDragInfoHandler(AssociatedObject, OnDragInfo);
}
protected override void OnDetaching()
{
base.OnDetaching();
RadDragAndDropManager.RemoveDragQueryHandler(AssociatedObject, OnDragQuery);
RadDragAndDropManager.RemoveDragInfoHandler(AssociatedObject, OnDragInfo);
}
private void OnDragQuery(object sender, DragDropQueryEventArgs e)
{
if (e.Options.Source is GridViewHeaderCell)
{
return;
}
var gridView = sender as RadGridView;
if (gridView != null)
{
var selectedItems = gridView.SelectedItems.ToList();
e.Options.Payload = selectedItems;
}
e.QueryResult = true;
e.Handled = true;
}
private void OnDragInfo(object sender, DragDropEventArgs e)
{
if (e.Options.Source is GridViewHeaderCell)
{
return;
}
var draggedItems = e.Options.Payload as IEnumerable;
if (e.Options.Status == DragStatus.DragInProgress)
{
var cue = new TreeViewDragCue();
cue.ItemsSource = draggedItems;
e.Options.DragCue = cue;
}
else if (e.Options.Status == DragStatus.DragComplete)
{
var source = this.AssociatedObject.ItemsSource as IList;
foreach (object draggedItem in draggedItems)
{
source.Remove(draggedItem);
}
}
}
}
public class AllowDropBehavior : Behavior<RadGridView>
{
protected override void OnAttached()
{
base.OnAttached();
RadDragAndDropManager.SetAllowDrop(AssociatedObject, true);
RadDragAndDropManager.AddDropQueryHandler(AssociatedObject, OnDropQuery);
RadDragAndDropManager.AddDropInfoHandler(AssociatedObject, OnDropInfo);
}
protected override void OnDetaching()
{
base.OnDetaching();
RadDragAndDropManager.SetAllowDrop(AssociatedObject, false);
RadDragAndDropManager.RemoveDropQueryHandler(AssociatedObject, OnDropQuery);
RadDragAndDropManager.RemoveDropInfoHandler(AssociatedObject, OnDropInfo);
}
private void OnDropQuery(object sender, DragDropQueryEventArgs e)
{
if (e.Options.Source is GridViewHeaderCell)
{
return;
}
var draggedItems = e.Options.Payload as ICollection;
bool result = draggedItems.Cast<object>().All((object item) => item is string);
e.QueryResult = true;
e.Handled = true;
}
private void OnDropInfo(object sender, DragDropEventArgs e)
{
var gridView = e.Options.Destination as RadGridView;
var draggedItems = e.Options.Payload as ICollection;
var cue = e.Options.DragCue as TreeViewDragCue;
if (e.Options.Status == DragStatus.DropPossible)
{
cue.IsDropPossible = true;
}
else if (e.Options.Status == DragStatus.DropImpossible)
{
cue.IsDropPossible = false;
}
else if (e.Options.Status == DragStatus.DropComplete)
{
var items = gridView.ItemsSource as IList;
foreach (object draggedItem in draggedItems)
{
items.Add(draggedItem);
}
}
}
}

<UserControl x:Class="SL.MainPage"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
             xmlns:local="clr-namespace:SL"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="400">
    <UserControl.Resources>
        <Style TargetType="telerik:GridViewRow" >
            <Setter Property="telerik:RadDragAndDropManager.AllowDrag" Value="True" />
        </Style>
        <local:MainPageViewModel x:Key="viewModel"/>
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot" Background="White" DataContext="{StaticResource viewModel}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <telerik:RadGridView Grid.Row="0"
                             ItemsSource="{Binding Teams}"
                             SelectionMode="Multiple"
                             Margin="5">
            <i:Interaction.Behaviors>
                <local:AllowDragBehavior/>
            </i:Interaction.Behaviors>
        </telerik:RadGridView>
        <telerik:RadGridView Grid.Column="1"
                             ItemsSource="{Binding Teams2}"
                             Margin="5">
            <i:Interaction.Behaviors>
                <local:AllowDropBehavior/>
            </i:Interaction.Behaviors>
        </telerik:RadGridView>
    </Grid>
</UserControl>

Teams and Teams 2 are just observablecollection of strings. It's just totally weird.
0
Espen
Top achievements
Rank 1
answered on 02 Aug 2011, 11:38 AM
Also...when I'm moving the behaviors to my actual solution...drop doesn't work at all :( Is there any special consideration I need to do when the drag and drop is in a childwindow?
0
Ivan Ivanov
Telerik team
answered on 03 Aug 2011, 10:15 AM
Hello Espen Berglund,

I have prepared a sample project utilizing your behaviors, without appliying any significant changes to them, and it is working just fine. I am attaching it for your reference. Would you please, confirm which version of RadControls you are using? Some more info on the issue or a simple demo project would be highly appreciated. Do you experience any exceptions when the behavior fails?

Greetings,
Ivan Ivanov
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>
Tags
GridView
Asked by
Espen
Top achievements
Rank 1
Answers by
Ivan Ivanov
Telerik team
Espen
Top achievements
Rank 1
Share this question
or