在WPF C#drag&;中查找对象的列表框名称或类型;滴

在WPF C#drag&;中查找对象的列表框名称或类型;滴,c#,wpf,drag-and-drop,listbox,observablecollection,C#,Wpf,Drag And Drop,Listbox,Observablecollection,我正在写一个应用程序,我可以从一个列表拖动到另一个列表,但我也可以从同一个列表拖动到它自己。是否需要知道哪个列表是源列表(我指的是我从哪个列表中拖动的列表)或者我拖动了哪些数据? 我有两个列表:lbTwo和lbOne,以及两个数据(每个可观察集合列表一个):firstDataType和secondDataType。 我发现获取数据的唯一方法是(但我更喜欢获取列表名,而不是它包含的数据): 但它不是很优雅,我也知道它的全名(Myapp.firstDataType) 谢谢 下面是我的代码的样子: X

我正在写一个应用程序,我可以从一个列表拖动到另一个列表,但我也可以从同一个列表拖动到它自己。是否需要知道哪个列表是源列表(我指的是我从哪个列表中拖动的列表)或者我拖动了哪些数据? 我有两个列表:lbTwo和lbOne,以及两个数据(每个可观察集合列表一个):firstDataType和secondDataType。 我发现获取数据的唯一方法是(但我更喜欢获取列表名,而不是它包含的数据):

但它不是很优雅,我也知道它的全名(Myapp.firstDataType)


谢谢

下面是我的代码的样子:

Xaml:

这是跨Listbox的DragDrop的实际实现&在Listbox中重新排列

    private void Common_Drop(object sender, DragEventArgs e)
    {
        var destLisBox = sender as ListBox;
        var dragDropData = e.Data.GetData(typeof(DragDropData)) as DragDropData;

        if (dragDropData != null)
        {
            var dataWhereToInsert = GetDataFromListBox(destLisBox, e.GetPosition(destLisBox));

            if (dataWhereToInsert != null)
            {
                var insertAtIndex = destLisBox.Items.IndexOf(dataWhereToInsert);
                if (insertAtIndex >= 0)
                {
                    dragDropData.DragStartSource.Items.Remove(dragDropData.ActualData);
                    destLisBox.Items.Insert(insertAtIndex, dragDropData.ActualData);
                }
            }
            else
            {
                dragDropData.DragStartSource.Items.Remove(dragDropData.ActualData);
                destLisBox.Items.Add(dragDropData.ActualData);
            }
        }
    }   

    private static object GetDataFromListBox(ListBox source, Point point)
    {
        UIElement element = source.InputHitTest(point) as UIElement;
        if (element != null)
        {
            object data = DependencyProperty.UnsetValue;
            while (data == DependencyProperty.UnsetValue)
            {
                data = source.ItemContainerGenerator.ItemFromContainer(element);
                if (data == DependencyProperty.UnsetValue)
                {
                    element = VisualTreeHelper.GetParent(element) as UIElement;
                }
                if (element == source)
                {
                    return null;
                }
            }
            if (data != DependencyProperty.UnsetValue)
            {
                return data;
            }
        }
        return null;
    }

    private void Common_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        var srcListBox = sender as ListBox;
        if (srcListBox != null)
        {
            var data = GetDataFromListBox(srcListBox, e.GetPosition(srcListBox));

            if (data != null)
            {
                var dragDropData = new DragDropData
                {
                    ActualData = data,
                    DragStartSource = srcListBox
                };

                DragDrop.DoDragDrop(srcListBox, dragDropData, DragDropEffects.Move);
            }
        }
    }
请注意,我将data&sender包装到以下类中:

public class DragDropData
{
    public object ActualData { get; set; }
    public ListBox DragStartSource { get; set; }
}
令人惊讶的是:GetDataFromListBox方法来自:
[

错误的建议-但是在xaml中为两个列表框添加名称,然后在Control.Name的代码测试中添加名称。我已经给了列表框名称…但是Control.Name…没有选项(没有这样的变量)通过Control.Name-我的意思是-在你上面的代码中,它将是parent.Name.谢谢,我找到了dragSource.Name做同样的:)谢谢,这很好!你知道我如何添加一个可视指示器,以便我可以看到我将其放置在哪里吗?@Ben-我没有任何这样做的经验-这可能会有帮助:
private void LoadData()
    {
        var lstbox1Data = new string[] { "One", "Two", "Three", "Four", "Five" };
        var lstbox2Data = new string[] { "Alpha", "Beta", "Theta", "Gamma", "Phi" };

        foreach (var item in lstbox1Data)
        {
            LeftBox.Items.Add(item);
        }

        foreach (var item in lstbox2Data)
        {
            RightBox.Items.Add(item);
        }
    }        
    private void Common_Drop(object sender, DragEventArgs e)
    {
        var destLisBox = sender as ListBox;
        var dragDropData = e.Data.GetData(typeof(DragDropData)) as DragDropData;

        if (dragDropData != null)
        {
            var dataWhereToInsert = GetDataFromListBox(destLisBox, e.GetPosition(destLisBox));

            if (dataWhereToInsert != null)
            {
                var insertAtIndex = destLisBox.Items.IndexOf(dataWhereToInsert);
                if (insertAtIndex >= 0)
                {
                    dragDropData.DragStartSource.Items.Remove(dragDropData.ActualData);
                    destLisBox.Items.Insert(insertAtIndex, dragDropData.ActualData);
                }
            }
            else
            {
                dragDropData.DragStartSource.Items.Remove(dragDropData.ActualData);
                destLisBox.Items.Add(dragDropData.ActualData);
            }
        }
    }   

    private static object GetDataFromListBox(ListBox source, Point point)
    {
        UIElement element = source.InputHitTest(point) as UIElement;
        if (element != null)
        {
            object data = DependencyProperty.UnsetValue;
            while (data == DependencyProperty.UnsetValue)
            {
                data = source.ItemContainerGenerator.ItemFromContainer(element);
                if (data == DependencyProperty.UnsetValue)
                {
                    element = VisualTreeHelper.GetParent(element) as UIElement;
                }
                if (element == source)
                {
                    return null;
                }
            }
            if (data != DependencyProperty.UnsetValue)
            {
                return data;
            }
        }
        return null;
    }

    private void Common_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        var srcListBox = sender as ListBox;
        if (srcListBox != null)
        {
            var data = GetDataFromListBox(srcListBox, e.GetPosition(srcListBox));

            if (data != null)
            {
                var dragDropData = new DragDropData
                {
                    ActualData = data,
                    DragStartSource = srcListBox
                };

                DragDrop.DoDragDrop(srcListBox, dragDropData, DragDropEffects.Move);
            }
        }
    }
public class DragDropData
{
    public object ActualData { get; set; }
    public ListBox DragStartSource { get; set; }
}