开发者

Finding the selected item in an ItemsControl when the PreviewMouseLeftButtonDownCommand RelayCommand\EventToCommand fires

My app is wpf mvvm, using RelayCommand\EventToCommand bindings for the events. My app does some typical drag & drop from a ListBox onto an ItemsControl (it is actually an image control with an ItemsControl on top, that is holding the dropped items). The ListBox is populated with a vm ObservableCollection. And the ItemsControl is also an ObservableCollection that I insert the dropped MyObj items into.

When I drag items from the ListBox and drop them in to\on to the ItemsControl\image it all works fine. In the PreviewMouseLeftButtonDownCommand I use the System.Windows.Media.VisualTreeHelper to recursively walk up the visual tree, so when I drag from the ListBox I can find the MyObj item that is being dragged. But when I try drag an item from the ItemsControl the code does not work. All I can get back is the DataTemplate conversion of the item (a lable). So my question is; how do I get the selected item from my ItemsControl when the PreviewMouseLeftButtonDownCommand RelayCommand\EventToCommand fires?

the 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", tagDr开发者_如何学编程agging);
    DragDrop.DoDragDrop(item, dragData, DragDropEffects.Move);
    }
});


Here's the code I've used in the past:

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 class

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;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜