我试图从列表视图中删除Order类型的Object,该列表视图是从同一listview控件中的另一个listview发送的。这些列表视图的xaml是这样的:
<ListView x:Name="listView" HorizontalAlignment="Left" Height="448" ItemsSource="{Binding}" VerticalAlignment="Top" Width="664" Margin="121,43,-2,0">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" Height="100"></StackPanel>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<Button x:Name="Up" Content="^" Click="Up_Click"/>
<Button x:Name="Down" Content="v" Click="Down_Click"/>
<TextBlock Text="{Binding Chauffeur.Naam}" Width="50"/>
<ListView x:Name="Swagnek" ItemsSource="{Binding Orders}" Height="50" AllowDrop="True" DragEnter="ListView_DragEnter" PreviewDragOver="ListView_PreviewDragOver" Drop="ListView_Drop">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock x:Name="HoiDoei" HorizontalAlignment="Stretch" Text="{Binding ID}" MouseRightButtonDown="HoiDoei_MouseRightButtonDown" MouseLeftButtonDown="HoiDoei_MouseLeftButtonDown" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
所以我试图在两个列表视图之间拖放订单。 问题是,当我放弃它时,我必须从列表视图中删除拖动和删除顺序,我拖动它。
我正在使用这个c#代码。
private void ListView_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var tb = (TextBlock)sender;
DragDrop.DoDragDrop(this, (Order)tb.DataContext, DragDropEffects.Copy);
}
private void ListView_DragEnter(object sender, DragEventArgs e)
{
e.Effects = DragDropEffects.Move;
}
private void ListView_PreviewDragOver(object sender, DragEventArgs e)
{
e.Handled = true;
}
private void ListView_Drop(object sender, DragEventArgs e)
{
var lv = sender as ListView;
if (lv.ItemsSource == null)
lv.ItemsSource = new ObservableCollection<Order>();
var order = e.Data.GetData(typeof(Order)) as Order;
var items = lv.ItemsSource as ObservableCollection<Order>;
items.Add(order);
orderlist.Remove(order);
}
也可以从列表框拖动到其中一个列表视图(不包含在代码中)
那是
的原因 orderlist.Remove(order);
在那里
答案 0 :(得分:1)
您从拖动对象的ListView的ItemsSource属性也应设置或绑定到ObservableCollection<Order>
。然后,您应该能够在引用它之后从该集合中删除已删除的项目:
ObservableCollection<Order> _source;
private void ListView_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var tb = (TextBlock)sender;
ListView parentListView = FindParent<ListView>(tb);
if (parentListView != null)
_source = parentListView.ItemsSource as ObservableCollection<Order>;
DragDrop.DoDragDrop(this, (Order)tb.DataContext, DragDropEffects.Copy);
}
public static T FindParent<T>(DependencyObject dependencyObject) where T : DependencyObject
{
var parent = VisualTreeHelper.GetParent(dependencyObject);
if (parent == null) return null;
var parentT = parent as T;
return parentT ?? FindParent<T>(parent);
}
private void ListView_Drop(object sender, DragEventArgs e)
{
var lv = sender as ListView;
if (lv.ItemsSource == null)
lv.ItemsSource = new ObservableCollection<Order>();
var order = e.Data.GetData(typeof(Order)) as Order;
var items = lv.ItemsSource as ObservableCollection<Order>;
items.Add(order);
orderlist.Remove(order);
if (_source != null && _source.Contains(order))
_source.Remove(order);
}
答案 1 :(得分:-1)
而不是将TextBlock的DataContext作为数据传递给DragAndDrop动作,而不是传递TextBlock本身。
private void ListView_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DragDrop.DoDragDrop(this, (TextBlock)sender, DragDropEffects.Copy);
}
然后在drop处理程序中,您现在可以访问包含TextBlock的ItemsControl(通过静态帮助器方法),并使用ItemsControl可以获取基础ItemsSource集合。
private void ListView_Drop(object sender, DragEventArgs e)
{
var lv = (ListView)sender;
if (lv.ItemsSource == null)
{
lv.ItemsSource = new ObservableCollection<Order>();
}
var tb = (TextBlock)e.Data.GetData(typeof(TextBlock));
var order = (Order)tb.DataContext;
var container = ItemsControl.ContainerFromElement(null, tb);
var control = ItemsControl.ItemsControlFromItemContainer(container);
(control.ItemsSource as ICollection<Order>)?.Remove(order);
(lv.ItemsSource as ObservableCollection<Order>)?.Add(order);
orderlist.Remove(order);
}
这将从最初包含在其中的ItemsControl中删除拖动的订单。