WPF拖放可重新排列的wrappanel项目

时间:2015-10-01 00:21:54

标签: c# wpf wpf-controls

我需要一些能够安排/排序物品的东西,例如一个包裹物,但是你可以通过拖拉机重新排列物品。下降。我试图尽可能快地将其原型化,所以我现在只想找一些简单或者愚蠢的东西。我一直在寻找这个地方,但我能找到的唯一答案需要相对大量的工作才能实现。

如果可能,我宁愿不使用现有的库。

修改

澄清:我需要拖拽和放大放下以及在一个控件中自动重新排列机制,该控件可以安排像wrappanel这样的项目。

自发布此问题以来,我发现两篇帖子/文章似乎很合适,但只有在合并的情况下。

Wrappanel:http://www.codeproject.com/Articles/18561/Custom-ListBox-Layout-in-WPF 拖放& amp;安排:WPF C#: Rearrange items in listbox via drag and drop

当我完成所有工作时,我会在答案中发布我的代码。

1 个答案:

答案 0 :(得分:0)

花了一些时间,但在Micky的建议之后我能够弄明白。我在这里发布一个答案供将来参考,如果有其他人正在寻找这个机制。以下代码片段应该只需将它们粘贴到适当的文件中即可。

以下是有效的方法:

使ListBox通过自定义/默认样式排列类似WrapPanel的项目(我的名称为Default.xaml)。

<Style TargetType="{x:Type ListBox}">
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <!--'WrapPanel' can be replaced with other controls if you want it to display differently-->
                <WrapPanel/>
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
    <Setter Property="ItemTemplate">
        <Setter.Value>
            <DataTemplate>
                <!--Controls in each item-->
                <local:DocPage />
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

<小时/> 设置ListBox控件(即MainWindow.xaml):

<ListBox x:Name="lbx_Pages" AllowDrop="True" DragEnter="lbx_Pages_DragEnter" PreviewMouseLeftButtonDown="lbx_Pages_PreviewMouseLeftButtonDown" PreviewMouseMove="lbx_Pages_PreviewMouseMove" Drop="lbx_Pages_PagesDrop"/>

<小时/> 篡改.cs文件中的控件(即MainWindow.xaml.cs):

private Point dragStartPoint;
// Bindable list of pages (binding logic omitted-out of scope of this post)
private static ObservableCollection<DocPage> pages = new ObservableCollection<DocPage>();

// Find parent of 'child' of type 'T'
public static T FindParent<T>(DependencyObject child) where T : DependencyObject
{
    do
    {
        if (child is T)
            return (T)child;
        child = VisualTreeHelper.GetParent(child);
    } while (child != null);
    return null;
}

private void lbx_Pages_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    dragStartPoint = e.GetPosition(null);
}

private void lbx_Pages_PreviewMouseMove(object sender, MouseEventArgs e)
{

    ListBoxItem item = null;
    DataObject dragData;
    ListBox listBox;
    DocPage page;

    // Is LMB down and did the mouse move far enough to register a drag?
    if (e.LeftButton == MouseButtonState.Pressed &&
        (Math.Abs(dragStartPoint.X - e.GetPosition(null).X) > SystemParameters.MinimumHorizontalDragDistance ||
        Math.Abs(dragStartPoint.Y - e.GetPosition(null).Y) > SystemParameters.MinimumVerticalDragDistance))
    {
        // Get the ListBoxItem object from the object being dragged
        item = FindParent<ListBoxItem>((DependencyObject)e.OriginalSource);

        if (null != item)
        {
            listBox = sender as ListBox;
            page = (DocPage)listBox.ItemContainerGenerator.ItemFromContainer(item);
            dragData = new DataObject("pages", page);

            DragDrop.DoDragDrop(item, dragData, DragDropEffects.Move);
        }
    }
}

private void lbx_Pages_PagesDrop(object sender, DragEventArgs e)
{
    if (!e.Data.GetDataPresent("pages"))
        return;

    DocPage draggedItem = e.Data.GetData("pages") as DocPage;
    // Hit-test needed for rearranging items in the same ListBox
    HitTestResult hit = VisualTreeHelper.HitTest((ListBox)sender, e.GetPosition((ListBox)sender));
    DocPage target = (DocPage)FindParent<ListBoxItem>(hit.VisualHit).DataContext;


    int removeIdx = lbx_Pages.Items.IndexOf(draggedItem);
    int targetIdx = lbx_Pages.Items.IndexOf(target);

    if(removeIdx < targetIdx)
    {
        pages.Insert(targetIdx + 1, draggedItem);
        pages.RemoveAt(removeIdx);
    }
    else
    {
        removeIdx++;
        if(pages.Count+1 > removeIdx)
        {
            pages.Insert(targetIdx, draggedItem);
            pages.RemoveAt(removeIdx);
        }
    }
}

private void lbx_Pages_DragEnter(object sender, DragEventArgs e)
{
    if (!e.Data.GetDataPresent("pages") || sender == e.Source)
        e.Effects = DragDropEffects.None;
}