简体   繁体   English

WPF拖放轮播

[英]WPF drag and drop Carousel

I´m new in WPF and I´m trying to do a drag and drop with carousel. 我是WPF的新手,我正在尝试对旋转木马进行拖放。

First, I´ve seen a example with listview. 首先,我看了一个有关listview的例子。 The example is this: http://wpftutorial.net/DragAndDrop.html , I´ve tried it and it is correct. 示例是这样的: http ://wpftutorial.net/DragAndDrop.html,我已经尝试过了,而且是正确的。

But my problem is that when I want to use a Carousel I don´t know to get the item selected when I do a click in the element I want to move. 但是我的问题是,当我想使用旋转木马时,单击要移动的元素时我不知道要选择哪个项目。 In the example is this function: 在示例中是该函数:

private void List_MouseMove(object sender, MouseEventArgs e)
{
    // Get the current mouse position
    Point mousePos = e.GetPosition(null);
    Vector diff = startPoint - mousePos;

    if (e.LeftButton == MouseButtonState.Pressed &&
        Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
        Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance )
    {
        // Get the dragged ListViewItem
        ListView listView = sender as ListView;
        ListViewItem listViewItem = 
            FindAnchestor<ListViewItem>((DependencyObject)e.OriginalSource);

        // Find the data behind the ListViewItem
        Contact contact = (Contact)listView.ItemContainerGenerator.
            ItemFromContainer(listViewItem);

        // Initialize the drag & drop operation
        DataObject dragData = new DataObject("myFormat", contact );
        DragDrop.DoDragDrop(listViewItem, dragData, DragDropEffects.Move);
    } 
}

I´m using this code in xaml file: 我在xaml文件中使用以下代码:

<dxca:CarouselItemsControl x:Name="_carouselName" 
                                    PreviewMouseLeftButtonDown="List_PreviewMouseLeftButtonDown" 
                                    PreviewMouseMove="List_MouseMove" >

I need to get the object I want to drag, in the example is contact. 我需要获取要拖动的对象,在示例中为contact。

// Find the data behind the ListViewItem
Contact contact = (Contact)listView.ItemContainerGenerator.ItemFromContainer(listViewItem);

Any idea? 任何想法?

If you're new to WPF and you're attempting something as tricky as this, then you're either very clever or very... well... something else. 如果您是WPF的新手,并且正在尝试像这样棘手的事情,那么您要么非常聪明,要么就非常...很好。

Try something like this: 尝试这样的事情:

private void ListView_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    UIElement uiElement = (UIElement)e.Source;
    HitTestResult hitTestResult = VisualTreeHelper.HitTest(uiElement,  
e.GetPosition(null));
    ListBoxItem listBoxItemUnderMouse = hitTestResult.VisualHit.
GetParentOfType<ListBoxItem>();
    if (listBoxItemUnderMouse != null)
    {
        // Do your stuff here
    }
}        

GetParentOfType is a static extension helper method that I created: GetParentOfType是我创建的static extension帮助器方法:

public static T GetParentOfType<T>(this DependencyObject element) where T : DependencyObject
{
    Type type = typeof(T);
    if (element == null) return null;
    DependencyObject parent = VisualTreeHelper.GetParent(element);
    if (parent == null && ((FrameworkElement)element).Parent is DependencyObject) 
parent = ((FrameworkElement)element).Parent;
    if (parent == null) return null;
    else if (parent.GetType() == type || parent.GetType().IsSubclassOf(type)) return 
parent as T;
    return GetParentOfType<T>(parent);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM