首页 > 解决方案 > 在 GridView 内的分组集合中启用组之间的拖放

问题描述

我想在 WPF 应用程序中实现拖动。我在我的 Windows 8.1 应用程序中使用 MVVM 和 Prism

这是设置。在我的 ViewModel 中,我有一个包含分组项的 Observable 集合。在我的视图中,我有 Grid,在我启用 AllowDrop="True" CanReorderItems="True" CanDragItems="True"了拖动的网格 GridView 内。

当我在网上搜索时,建议使用大多数答案,<Core:EventTriggerBehavior EventName="Drop">因为我使用的是 MVVM 并且后面的代码中没有代码。在我的 ViewModel 中,我实现了适当的DelegateCommand和方法。

之后,我设法捕获了正在拖动的对象,但我不知道如何知道该项目被丢弃在哪个组中。

有人可以指出我需要更改代码以了解该组吗?我应该使用什么样的方法/类/属性?如果需要,我什至对完全不同的方法持开放态度

我想要达到的目标。

例子:

A组 B组 ......
一个 D --------
--------
C F --------

拖后

A组 B组 ......
D --------
C --------
一个 --------
F --------

旁注 组的数量是不同的,它基于集合中的项目。

一些代码:

MainClass - 包含组头和组的 ObservableCollection 的类(示例中为组 A) Collection - 对象的实际集合(B,C)

所以基本上是集合,里面有集合。

<prism:VisualStateAwarePage.Resources>
<CollectionViewSource x:Key="objectsCVS" IsSourceGrouped="True" ItemsPath="Collection" Source="{Binding Path=MainClass}" />
</prism:VisualStateAwarePage.Resources>

<Grid>
    <GridView Grid.Row="1" HorizontalContentAlignment="Stretch" AllowDrop="True" CanReorderItems="True" CanDragItems="True" VerticalContentAlignment="Stretch" VerticalAlignment="Stretch"
                              ItemsSource="{Binding Source={StaticResource objectsCVS}}" SelectedItem="{Binding Path=SelectedObject, Mode=TwoWay}"
                              ItemTemplateSelector="{StaticResource objectTemplateSelector}" SelectionMode="Single" IsSynchronizedWithCurrentItem="False">

    <Interactivity:Interaction.Behaviors>
        <Core:EventTriggerBehavior EventName="Drop">
            
            <Core:InvokeCommandAction Command="{Binding Path=DropCommand}" CommandParameter="{Binding Path=SelectedObject}" />
    
            <Core:InvokeCommandAction Command="{Binding Path=DropCommand}"/>
        </Core:EventTriggerBehavior>
    </Interactivity:Interaction.Behaviors>

    </GridView>
    <GridView.GroupStyle>
    <GroupStyle HidesIfEmpty="True">
        <GroupStyle.HeaderTemplate>
            <DataTemplate>
                <TextBlock x:Name="textB" Foreground="Black">
                    <Run Text="{Binding Path=someHeader}" />             
                </TextBlock>
            </DataTemplate>
        </GroupStyle.HeaderTemplate>
    </GridView.GroupStyle>
</Grid>

查看模型

我正在使用 VB

Private ReadOnly theDropCommand As New DelegateCommand(Of Object)(AddressOf DoDrop)
    Public ReadOnly Property DropCommand As ICommand
        Get
            Return theDropCommand
        End Get
    End Property


Sub DoDrop(content As Object)

    //For test purposes I left it Object instead of the actual type
    ???

End Sub

标签: wpfmvvmgroupingprismdrag

解决方案


推荐阅读