I've having trouble understanding a ListBox
ItemsSource
binding error I'm getting. I followed the details on the Gong WPF.DragDrop GitHub project but I'm getting this error on my binding path:
The project here is what I'm working on and shows the issue. I've extended a little by using a generic type on my observable item so i can make the ListBoxItemViewModel
reusable but I found that going without the generic type it still fails.
This is the XAML used for the ListBox
.
<ListBox Width="300px" x:Name="clothingItems" Margin="10px" AllowDrop="True"
ItemsSource="{Binding Items}"
dd:DragDrop.IsDragSource="True"
dd:DragDrop.IsDropTarget="True"
dd:DragDrop.DragHandler="{Binding}"/>
This is the code for my ListBoxViewModel
.
public class ListBoxViewModel<TItemVm> : IDropTarget
where TItemVm : class, IDropTargetItemViewModel<TItemVm>
{
public ObservableCollection<TItemVm> Items = new ObservableCollection<TItemVm>();
public void DragOver(IDropInfo dropInfo)
{
var sourceItem = dropInfo.Data as TItemVm;
var targetItem = dropInfo.TargetItem as TItemVm;
if (sourceItem != null && targetItem != null && targetItem.CanAcceptChildren)
{
dropInfo.DropTargetAdorner = DropTargetAdorners.Highlight;
dropInfo.Effects = System.Windows.DragDropEffects.Copy;
}
}
public void Drop(IDropInfo dropInfo)
{
var sourceItem = dropInfo.Data as TItemVm;
var targetItem = dropInfo.TargetItem as TItemVm;
targetItem.Children.Add(sourceItem);
}
}
ListBoxViewModel
has a public ObservableCollection
property called Items
and an instance is assigned as the DataContext so why wouldn't ItemsSource="{Binding Items}"
resolve?