ListBox ItemsSource 绑定路径错误。我错过了什么?
ListBox ItemsSource binding path error. What am I missing?
我无法理解我遇到的 ListBox
ItemsSource
绑定错误。我遵循了 Gong WPF.DragDrop GitHub 项目的详细信息,但我在绑定路径上收到此错误:
项目 here 是我正在处理的项目,它显示了问题。我通过在我的可观察项上使用通用类型进行了一些扩展,因此我可以使 ListBoxItemViewModel
可重用,但我发现如果没有通用类型它仍然会失败。
这是 XAML 用于 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}"/>
这是我的 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
有一个名为 Items
的 public ObservableCollection
属性 并且一个实例被分配为 DataContext 那么为什么 ItemsSource="{Binding Items}"
解决?
问题是 ListBoxViewModel<TItemVm>
中的 Items
是 字段 ,而不是 属性。
public ObservableCollection<TItemVm> Items = new ObservableCollection<TItemVm>();
像下面这样更改成员,使其成为 属性 并启用绑定。
public ObservableCollection<TItemVm> Items { get; } = new ObservableCollection<TItemVm>();
有关可在 WPF 中使用哪些绑定源的详细信息,请参阅 Binding Source Types。
我无法理解我遇到的 ListBox
ItemsSource
绑定错误。我遵循了 Gong WPF.DragDrop GitHub 项目的详细信息,但我在绑定路径上收到此错误:
项目 here 是我正在处理的项目,它显示了问题。我通过在我的可观察项上使用通用类型进行了一些扩展,因此我可以使 ListBoxItemViewModel
可重用,但我发现如果没有通用类型它仍然会失败。
这是 XAML 用于 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}"/>
这是我的 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
有一个名为 Items
的 public ObservableCollection
属性 并且一个实例被分配为 DataContext 那么为什么 ItemsSource="{Binding Items}"
解决?
问题是 ListBoxViewModel<TItemVm>
中的 Items
是 字段 ,而不是 属性。
public ObservableCollection<TItemVm> Items = new ObservableCollection<TItemVm>();
像下面这样更改成员,使其成为 属性 并启用绑定。
public ObservableCollection<TItemVm> Items { get; } = new ObservableCollection<TItemVm>();
有关可在 WPF 中使用哪些绑定源的详细信息,请参阅 Binding Source Types。