使用 ItemSource 使用 ListView WPF 拖放失败
Drag and Drop with ListView WPF failed using ItemSource
我有 ListView
和 ItemSource:
public ObservableCollection<MyObject> List;
我的 ListView
装满了好几个东西。
现在我想添加通过 darg 更改我的 ListViewItems
的选项,所以我找到了这个解决方案:https://fxmax.wordpress.com/2010/10/05/wpf/
在我的项目中添加代码后,我只有一个导致崩溃的问题:
private void BeginDrag(MouseEventArgs e)
{
ListView listView = this.listView;
ListViewItem listViewItem = FindAnchestor<ListViewItem>((DependencyObject)e.OriginalSource);
if (listViewItem == null)
return;
// get the data for the ListViewItem
MyObject name = (MyObject)listView.ItemContainerGenerator.ItemFromContainer(listViewItem);
//setup the drag adorner.
InitialiseAdorner(listViewItem);
//add handles to update the adorner.
listView.PreviewDragOver += listView_PreviewDragOver;
listView.DragLeave += listView_DragLeave;
listView.DragEnter += listView_DragEnter;
DataObject data = new DataObject("myFormat", name);
DragDropEffects de = DragDrop.DoDragDrop(this.listView, data, DragDropEffects.Move);
//cleanup
listView.PreviewDragOver -= listView_PreviewDragOver;
listView.DragLeave -= listView_DragLeave;
listView.DragEnter -= listView_DragEnter;
if (_adorner != null)
{
AdornerLayer.GetAdornerLayer(listView).Remove(_adorner);
_adorner = null;
}
}
在这一行:
DragDropEffects de = DragDrop.DoDragDrop(this.listView, data, DragDropEffects.Move);
System.InvalidOperationException: 'Operation is not valid while
ItemsSource is in use. Access and modify elements with
ItemsControl.ItemsSource instead.'
我可以看出这是因为我正在使用 ItemSource
但不知道要更改什么。
有什么建议吗?
从您在其他站点上链接的示例代码开始,我做了一些更改并且能够修复解决方案以使用 ItemsSource。
所做的修改如下:
- 在 MainWindow.xaml
<ListView ItemsSource="{Binding List}"
x:Name="listView"
- 在 MainWindow.xaml.cs
private void ListViewDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent("myFormat"))
{
MyObject name = e.Data.GetData("myFormat") as MyObject;
ListViewItem listViewItem = FindAnchestor<ListViewItem>((DependencyObject)e.OriginalSource);
ObservableCollection<MyObject> myArray = listView.ItemsSource as ObservableCollection<MyObject>;
if (listViewItem != null && listViewItem.DataContext is MyObject)
{
MyObject dropLocation = (MyObject)listViewItem.DataContext;
int index = myArray.IndexOf(dropLocation);
if (index >= 0)
{
myArray.Remove(name);
myArray.Insert(index, name);
}
}
else
{
myArray.Remove(name);
myArray.Add(name);
}
}
}
...
private void BeginDrag(MouseEventArgs e)
{
ListView listView = this.listView;
ListViewItem listViewItem =
FindAnchestor<ListViewItem>((DependencyObject)e.OriginalSource);
if (listViewItem == null)
return;
MyObject selectedItem = (MyObject)listViewItem.DataContext;
//setup the drag adorner.
InitialiseAdorner(listViewItem);
//add handles to update the adorner.
listView.PreviewDragOver += ListViewDragOver;
listView.DragLeave += ListViewDragLeave;
listView.DragEnter += ListViewDragEnter;
DataObject data = new DataObject("myFormat", selectedItem);
DragDropEffects de = DragDrop.DoDragDrop(this.listView, data, DragDropEffects.Move);
//cleanup
listView.PreviewDragOver -= ListViewDragOver;
listView.DragLeave -= ListViewDragLeave;
listView.DragEnter -= ListViewDragEnter;
if (_adorner != null)
{
AdornerLayer.GetAdornerLayer(listView).Remove(_adorner);
_adorner = null;
}
}
更改本身使用 MyObject 的概念作为有效负载,而不是字符串,ObservableCollection 作为您进行更改的数组,而不是 IEnumerable。
希望这能解决您的问题。
我有 ListView
和 ItemSource:
public ObservableCollection<MyObject> List;
我的 ListView
装满了好几个东西。
现在我想添加通过 darg 更改我的 ListViewItems
的选项,所以我找到了这个解决方案:https://fxmax.wordpress.com/2010/10/05/wpf/
在我的项目中添加代码后,我只有一个导致崩溃的问题:
private void BeginDrag(MouseEventArgs e)
{
ListView listView = this.listView;
ListViewItem listViewItem = FindAnchestor<ListViewItem>((DependencyObject)e.OriginalSource);
if (listViewItem == null)
return;
// get the data for the ListViewItem
MyObject name = (MyObject)listView.ItemContainerGenerator.ItemFromContainer(listViewItem);
//setup the drag adorner.
InitialiseAdorner(listViewItem);
//add handles to update the adorner.
listView.PreviewDragOver += listView_PreviewDragOver;
listView.DragLeave += listView_DragLeave;
listView.DragEnter += listView_DragEnter;
DataObject data = new DataObject("myFormat", name);
DragDropEffects de = DragDrop.DoDragDrop(this.listView, data, DragDropEffects.Move);
//cleanup
listView.PreviewDragOver -= listView_PreviewDragOver;
listView.DragLeave -= listView_DragLeave;
listView.DragEnter -= listView_DragEnter;
if (_adorner != null)
{
AdornerLayer.GetAdornerLayer(listView).Remove(_adorner);
_adorner = null;
}
}
在这一行:
DragDropEffects de = DragDrop.DoDragDrop(this.listView, data, DragDropEffects.Move);
System.InvalidOperationException: 'Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead.'
我可以看出这是因为我正在使用 ItemSource
但不知道要更改什么。
有什么建议吗?
从您在其他站点上链接的示例代码开始,我做了一些更改并且能够修复解决方案以使用 ItemsSource。
所做的修改如下: - 在 MainWindow.xaml
<ListView ItemsSource="{Binding List}"
x:Name="listView"
- 在 MainWindow.xaml.cs
private void ListViewDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent("myFormat"))
{
MyObject name = e.Data.GetData("myFormat") as MyObject;
ListViewItem listViewItem = FindAnchestor<ListViewItem>((DependencyObject)e.OriginalSource);
ObservableCollection<MyObject> myArray = listView.ItemsSource as ObservableCollection<MyObject>;
if (listViewItem != null && listViewItem.DataContext is MyObject)
{
MyObject dropLocation = (MyObject)listViewItem.DataContext;
int index = myArray.IndexOf(dropLocation);
if (index >= 0)
{
myArray.Remove(name);
myArray.Insert(index, name);
}
}
else
{
myArray.Remove(name);
myArray.Add(name);
}
}
}
...
private void BeginDrag(MouseEventArgs e)
{
ListView listView = this.listView;
ListViewItem listViewItem =
FindAnchestor<ListViewItem>((DependencyObject)e.OriginalSource);
if (listViewItem == null)
return;
MyObject selectedItem = (MyObject)listViewItem.DataContext;
//setup the drag adorner.
InitialiseAdorner(listViewItem);
//add handles to update the adorner.
listView.PreviewDragOver += ListViewDragOver;
listView.DragLeave += ListViewDragLeave;
listView.DragEnter += ListViewDragEnter;
DataObject data = new DataObject("myFormat", selectedItem);
DragDropEffects de = DragDrop.DoDragDrop(this.listView, data, DragDropEffects.Move);
//cleanup
listView.PreviewDragOver -= ListViewDragOver;
listView.DragLeave -= ListViewDragLeave;
listView.DragEnter -= ListViewDragEnter;
if (_adorner != null)
{
AdornerLayer.GetAdornerLayer(listView).Remove(_adorner);
_adorner = null;
}
}
更改本身使用 MyObject 的概念作为有效负载,而不是字符串,ObservableCollection 作为您进行更改的数组,而不是 IEnumerable。
希望这能解决您的问题。