当PreviewMouseLeftButtonDownCommand RelayCommandEventToComma

本文关键字:RelayCommandEventToComma PreviewMouseLeftButtonDownCommand | 更新日期: 2023-09-27 18:05:05

我的应用程序是wpf mvvm,使用RelayCommand'EventToCommand绑定的事件。我的应用做了一些典型的拖拽;从ListBox拖放到ItemsControl上(它实际上是一个图像控件,上面有一个ItemsControl,用来保存被拖放的项目)。ListBox是用vm ObservableCollection填充的。ItemsControl也是一个ObservableCollection,我将掉落的MyObj项插入其中。

当我从列表框中拖动项目并将它们放入ItemsControl图像时,一切都很好。在PreviewMouseLeftButtonDownCommand中,我使用System.Windows.Media.VisualTreeHelper递归地遍历可视化树,所以当我从ListBox中拖动时,我可以找到正在拖动的MyObj项。但是当我尝试从ItemsControl中拖动一个项目时,代码不起作用。我所能得到的只是项目(一个标签)的DataTemplate转换。我的问题是;当PreviewMouseLeftButtonDownCommand RelayCommand'EventToCommand火灾时,我如何从我的ItemsControl中获得选定的项目?

vm 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);
    }
});

当PreviewMouseLeftButtonDownCommand RelayCommandEventToComma

这是我以前用过的代码:

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 Helper类

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;
    }
}
相关文章:
  • 没有找到相关文章