0

我正在使用与 Gong 解决方案拖放库一起提供的示例应用程序。该解决方案包括一个设置了 itemssource 和 displaymemberpath 的列表框。我已修改应用程序以包含 itemscontrol 和 itemTemplate。但该解决方案不再有效。DragInfo.cs 文件中存在异常。不确定在这里发布是否正确。但是有人可以帮我解决这个问题。示例代码非常基本。

<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();
    }

Window 的 dataContext,被设置为 WindowViewModel 的一个实例。此代码与 Gong 库一起提供,也是代码项目的一部分。 http://www.codeproject.com/Articles/43614/Drag-and-Drop-in-WPF

原始代码看起来像这样

<ListBox Grid.Column="1" ItemsSource="{Binding Schools.CurrentItem.Pupils}" DisplayMemberPath="FullName"
             dd:DragDrop.IsDragSource="True" dd:DragDrop.IsDropTarget="True"/>
4

1 回答 1

2

如果您还没有弄清楚这一点 - 看起来您注释掉了它检查您拖动的东西是否是 SchoolView 的位置。由于您使用的是 DropTargetAdorners.Highlight 它试图突出显示您正在拖动的内容。由于没有任何内容,您会收到空引用错误。所以也许回到这个?

public void DragOver(DropInfo dropInfo)
{
    if (dropInfo.Data is PupilViewModel) && dropInfo.TargetItem is SchoolViewModel)
    {
        dropInfo.DropTargetAdorner = DropTargetAdorners.Highlight;
        dropInfo.Effects = DragDropEffects.Move;
    }
}
于 2014-01-04T14:59:47.033 回答