0
votes

I am using the sample application provided along with the Gong solutions drag drop library. The solution includes a listbox having itemssource and displaymemberpath set. I have modified the application to include an itemscontrol and itemTemplate. But the solution no longer works. There is an exception in the DragInfo.cs file. Not sure if posting it here is correct. But can someone help me with this. The sample code is pretty basic.

<ItemsControl 
        dragDropFramework:DragDrop.IsDragSource="True"
        Grid.Column="0" ItemsSource="{Binding Pupils}">            
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Label Content="{Binding FullName}" BorderBrush="Brown" BorderThickness="2"  Margin="2"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

<ItemsControl ItemsSource="{Binding Schools}"
        dragDropFramework:DragDrop.DropHandler="{Binding}"
        dragDropFramework:DragDrop.IsDropTarget="True"
        Grid.Column="1">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Label Content="{Binding Name}" BorderBrush="Brown" BorderThickness="2"  Margin="2"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

class PupilViewModel
{
    public string FullName { get; set; }
}

internal class WindowViewModel : IDropTarget
{
    public ICollectionView Schools { get; private set; }
    public ICollectionView Pupils { get; private set; }

    public SideWindowViewModel()
    {
        var pupils = new ObservableCollection<PupilViewModel>
            {
                new PupilViewModel { FullName = "Alex Thompson" },
                new PupilViewModel { FullName = "Tabitha Smith" },
                new PupilViewModel { FullName = "Carl Pederson" },
                new PupilViewModel { FullName = "Sarah Jones" },
                new PupilViewModel { FullName = "Paul Lowcroft" }
            };

        this.Pupils = CollectionViewSource.GetDefaultView(pupils);
        var schools = new SchoolViewModel { Name = "FirstSchool", Pupils = new ObservableCollection<PupilViewModel>() };
        this.Schools = CollectionViewSource.GetDefaultView(schools);
    }

    public void DragOver(DropInfo dropInfo)
    {
        if (dropInfo.Data is PupilViewModel)// && dropInfo.TargetItem is SchoolViewModel)
        {
            dropInfo.DropTargetAdorner = DropTargetAdorners.Highlight;
            dropInfo.Effects = DragDropEffects.Move;
        }
    }

    public void Drop(DropInfo dropInfo)
    {
        throw new NotImplementedException();
    }

The Window's dataContext, is set to an instance of WindowViewModel. This code comes with Gong library also as a part of code project. http://www.codeproject.com/Articles/43614/Drag-and-Drop-in-WPF

original code looks like this

<ListBox Grid.Column="1" ItemsSource="{Binding Schools.CurrentItem.Pupils}" DisplayMemberPath="FullName"
             dd:DragDrop.IsDragSource="True" dd:DragDrop.IsDropTarget="True"/>
1
Is your itemsControl only a dragsource? some more information regarding the exception and changes might help..AsitK
hi @AsitK This one only acts as a source. I shall have another ItemsControl which shall be the target. The exception I get is a NullreferenceException.Sandepku
@Sandepku add your DataContext implementation for Schools and Pupils please.eran otzap
@eranotzap here it goes!Sandepku

1 Answers

2
votes

In case you haven't figured this one out yet - it looks like you commented out where it checks to see if the thing you are dragging over is a SchoolView. Since you are using DropTargetAdorners.Highlight it is trying to highlight what you are dragging over. Since there isn't anything you're getting the null reference error. So maybe go back to this?

public void DragOver(DropInfo dropInfo)
{
    if (dropInfo.Data is PupilViewModel) && dropInfo.TargetItem is SchoolViewModel)
    {
        dropInfo.DropTargetAdorner = DropTargetAdorners.Highlight;
        dropInfo.Effects = DragDropEffects.Move;
    }
}