【问题标题】:Gong Solutions Drag drop + using ItemsControl + query龚解决方案拖放+使用ItemsControl+查询
【发布时间】:2013-09-02 06:39:02
【问题描述】:

我正在使用与 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"/>

【问题讨论】:

  • 你的 itemsControl 只是一个拖动源吗?有关异常和更改的更多信息可能会有所帮助..
  • 嗨@AsitK 这只是一个来源。我将有另一个 ItemsControl 作为目标。我得到的异常是 NullreferenceException。
  • @Sandepku 请为学校和学生添加您的 DataContext 实现。
  • @eranotzap 来了!

标签: wpf drag-and-drop


【解决方案1】:

如果您还没有弄清楚这一点 - 看起来您注释掉了它检查您拖动的东西是否是 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;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-22
    • 2015-11-22
    • 2011-04-01
    • 2011-07-22
    相关资源
    最近更新 更多