我有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:'操作在以下时间无效 ItemsSource正在使用中。使用访问和修改元素 改为使用ItemsControl.ItemsSource。'
我看到这是因为我正在使用ItemSource
,但不知道要更改什么。
有什么建议吗?
答案 0 :(得分:1)
从您在另一个站点上链接的示例代码开始,我进行了一些更改,并能够修复该解决方案以与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)作为数组。
希望这可以解决您的问题。