在列表视图中拖动多个文件

时间:2018-12-11 08:52:30

标签: c# wpf listview

我尝试在ListView中执行拖放操作,“拖动单个文件/文件夹”工作正常,但是当我拖动多个文件时,它会占据单个文件,如果我设置了SelectionMode="Multiple",选择样式就不会Windows标准。

以下代码适用于从ListView拖动单个文件/文件夹

UI:

 <ListView  Width="Auto" Height="280" KeyDown="LeftFolderListing_KeyDown"  IsSynchronizedWithCurrentItem="True" AllowDrop="True"  PreviewMouseLeftButtonDown="LeftList_LeftButtonDown" DragEnter="LeftList_DragEnter" Drop="LeftList_DropEnter" PreviewMouseMove="LeftList_PreviewMouseMove"  SelectionMode="Extended"
                        util:GridViewSort.AutoSort="True" x:Name="LeftFolderListing" ItemContainerStyle="{StaticResource LeftListViewItemStyle}" ItemsSource="{Binding LeftFolderList, ElementName=Viewer, Mode=TwoWay}" MouseRightButtonDown="LeftFolderListing_MouseRightButtonDown" ContextMenuOpening="LeftFolderListing_ContextMenuOpening">
                        <ListView.ContextMenu>
                            <ContextMenu x:Name="LeftListContextMenu" StaysOpen="true" Background="WhiteSmoke">
                                <MenuItem Header="Create New Folder"   Click="LeftNewFolder_Click"/>
                                <MenuItem x:Name="LeftRename" Header="Rename"  Click= "LeftRename_Click" DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"/>
                                <MenuItem x:Name="LeftDelete" Header="Delete" Click="LeftDelete_Click" DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"/>
                            </ContextMenu>
                        </ListView.ContextMenu>
                        <ListView.View>
                            <GridView>
                                <GridViewColumn x:Name="LeftName" Width="140"  CellTemplate="{StaticResource NameWithIcon}" Header="Name" util:GridViewSort.PropertyName="DisplayNameSort" HeaderContainerStyle="{StaticResource hcs}"/>
                                <GridViewColumn x:Name="LeftSize" Width="90" DisplayMemberBinding="{Binding Size, Converter={StaticResource SizeFormatter}}" Header="Size" util:GridViewSort.PropertyName="SizeforSort" HeaderContainerStyle="{StaticResource hcs}" />
                                <GridViewColumn x:Name="LeftDate" Width="115"  DisplayMemberBinding="{Binding ModifiedDate}" Header="Date Modified" util:GridViewSort.PropertyName="ModifiedDateSort" HeaderContainerStyle="{StaticResource hcs}"/>
                            </GridView>
                        </ListView.View>
</ListView>

C#:

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

      private void LeftList_DragEnter(object sender, DragEventArgs e)
            {
                e.Effects = DragDropEffects.Move;
            }

     private void LeftList_PreviewMouseMove(object sender, MouseEventArgs e)
            {
                if (e.LeftButton == MouseButtonState.Pressed)
                {
                    Point position = e.GetPosition(null);

                    if (Math.Abs(position.X - _startPoint.X) > SystemParameters.MinimumHorizontalDragDistance ||
                        Math.Abs(position.Y - _startPoint.Y) > SystemParameters.MinimumVerticalDragDistance)
                    {
                        BeginDrag(this.LeftFolderListing,"TEST_DATA", e);
                    }
                }
            }

private void BeginDrag(ListView ObjList, string Data, MouseEventArgs e)
        {          

                ListViewItem objListViewItem = FindAnchestor<ListViewItem>((DependencyObject)e.OriginalSource);

                if (objListViewItem == null)             
                    return;

                DataObject _data = new DataObject("TEST_FORMAT", Data);
                DragDropEffects de = DragDrop.DoDragDrop(ObjList, _data, DragDropEffects.Move);



        }

1 个答案:

答案 0 :(得分:0)

我有一个DroppedFile方法,将其传递给SaveFile方法,在savefile方法中,您可以使用foreach遍历它们。

注意:它只是fileList

中的文件路径
public void DroppedFile(object sender, DragEventArgs e)
{      
    SaveFile((string[])e.Data.GetData(DataFormats.FileDrop, false));
}

private void SaveFile(string[] fileList)

这使我可以保存多个文件。