1

我的应用程序是 wpf mvvm,对事件使用 RelayCommand\EventToCommand 绑定。我的应用程序执行了一些典型的从 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);
    }
});
4

1 回答 1

2

这是我过去使用的代码:

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;
    }
}
于 2011-08-10T20:20:12.250 回答