gpt4 book ai didi

c# - 当 PreviewMouseLeftButtonDownCommand RelayCommand\EventToCommand 触发时,在 ItemsControl 中查找所选项目

转载 作者:行者123 更新时间:2023-12-03 10:30:17 24 4
gpt4 key购买 nike

我的应用程序是 wpf mvvm,对事件使用 RelayCommand\EventToCommand 绑定(bind)。我的应用程序从 ListBox 到 ItemsControl 进行了一些典型的拖放操作(它实际上是一个顶部带有 ItemsControl 的图像控件,用于保存已放置的项目)。 ListBox 填充有 vm ObservableCollection。 ItemsControl 也是一个 ObservableCollection,我将放置的 MyObj 项目插入其中。

当我从 ListBox 拖动项目并将它们放到\on 到 ItemsControl\image 时,一切正常。在 PreviewMouseLeftButtonDownCommand 中,我使用 System.Windows.Media.VisualTreeHelper 递归地向上走可视化树,因此当我从 ListBox 拖动时,我可以找到正在拖动的 MyObj 项。但是当我尝试从 ItemsControl 中拖动一个项目时,代码不起作用。我能得到的只是项目(标签)的 DataTemplate 转换。所以我的问题是;当 PreviewMouseLeftButtonDownCommand RelayCommand\EventToCommand 触发时,如何从我的 ItemsControl 中获取所选项目?

虚拟机 C#:

PreviewMouseLeftButtonDownCommand = new RelayCommand<MouseButtonEventArgs>(e =>
{
if (e.Source is ListBox)
{
// get dragged list box item
ListBox listBox = e.Source as ListBox;
ListBoxItem listBoxItem = VisualHelper.FindAncestor<ListBoxItem>((DependencyObject)e.OriginalSource);

// Find the data behind the listBoxItem
if (listBox == null || listBoxItem == null) return;

MyObj tag = (MyObj)listBox.ItemContainerGenerator.ItemFromContainer(listBoxItem);

// Initialize the drag & drop operation
DataObject dragData = new DataObject("myObj", tag);
DragDrop.DoDragDrop(listBoxItem, dragData, DragDropEffects.Move);
}
else if (e.Source is ItemsControl)
{
ItemsControl itemsControl = e.Source as ItemsControl;
object item = VisualHelper.FindAncestor<UIElement>((DependencyObject)e.OriginalSource);

if (itemsControl == null || item == null) return;

MyObj tag = (MyObj)itemsControl.ItemContainerGenerator.ItemFromContainer(item);


// Initialize the drag & drop operation
DataObject dragData = new DataObject("myObj", tagDragging);
DragDrop.DoDragDrop(item, dragData, DragDropEffects.Move);
}
});

最佳答案

这是我过去使用的代码:

private void DragSource_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// Get ItemsControl for object being dragged
if (sender is ItemsControl)
_sourceItemsControl = sender as ItemsControl;
else if (sender is Panel)
_sourceItemsControl = WPFHelpers.FindAncester<ItemsControl>(sender as Panel);

// Get ItemContainer for object being dragged
FrameworkElement sourceItemsContainer = _sourceItemsControl
.ContainerFromElement((Visual)e.OriginalSource) as FrameworkElement;

// Get data object for object being dragged
if (sourceItemsContainer == null)
_draggedObject = _sourceItemsControl.DataContext;
else if (sourceItemsContainer == e.Source)
_draggedObject = e.Source;
else
_draggedObject = sourceItemsContainer.DataContext;

}

WPF 助手类
public class WPFHelpers
{
public static T FindAncester<T>(DependencyObject current)
where T : DependencyObject
{
current = VisualTreeHelper.GetParent(current);

while (current != null)
{
if (current is T)
{
return (T)current;
}
current = VisualTreeHelper.GetParent(current);
};
return null;
}
}

关于c# - 当 PreviewMouseLeftButtonDownCommand RelayCommand\EventToCommand 触发时,在 ItemsControl 中查找所选项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7001607/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com