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

时间:2011-08-09 19:12:01

标签: c# wpf mvvm eventtocommand

我的应用是wpf mvvm,使用RelayCommand \ EventToCommand绑定事件。我的应用程序做了一些典型的拖累和放大从ListBox中删除到ItemsControl(它实际上是一个顶部有ItemsControl的图像控件,它保存掉落的项目)。 ListBox使用vm ObservableCollection填充。 ItemsControl也是一个ObservableCollection,我将已删除的MyObj项插入其中。

当我从ListBox中拖动项目并将它们放入到ItemsControl \ image的\ on时,一切正常。在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);
    }
});

1 个答案:

答案 0 :(得分: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;
    }
}