C# 更改ICollectionView中的项目组

C# 更改ICollectionView中的项目组,c#,wpf,mvvm-light,C#,Wpf,Mvvm Light,我有ICollectionView看起来像 public ICollectionView UsersCollectionView { get { var view = CollectionViewSource.GetDefaultView(this); view.GroupDescriptions.Add(new PropertyGroupDescription("SeriesName")); view.SortDescriptio

我有
ICollectionView
看起来像

public ICollectionView UsersCollectionView
{
    get
    {
        var view = CollectionViewSource.GetDefaultView(this);
        view.GroupDescriptions.Add(new PropertyGroupDescription("SeriesName"));
        view.SortDescriptions.Add(new SortDescription("CreationDate", ListSortDirection.Ascending));
        view.SortDescriptions.Add(new SortDescription("DocumentTypeId", ListSortDirection.Ascending));
        return view;
    }
}
我想使用拖放来更改项目系列名称,以及列表视图上的位置。例如,知道如何做吗

--- ScienceFiction
------------> Book1 
------------> Book2
--- History 
------------> Book3 
------------> Book4
如果我读了科幻小说第三册,输出应该是

--- ScienceFiction
------------> Book1 
------------> Book2
------------> Book3 
--- History 
------------> Book4
我使用的xaml代码如下:

    <UserControl.Resources>
    <Style x:Key="ContainerStyle" TargetType="{x:Type GroupItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Expander Header="{Binding Name}" IsExpanded="True">
                        <ItemsPresenter />
                    </Expander>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>
<Grid>
    <ListBox x:Name="lbPersonList" Margin="19,17,162,25" AlternationCount="2" ItemsSource="{Binding}">
        <ListBox.GroupStyle>
            <GroupStyle ContainerStyle="{StaticResource ContainerStyle}"/>
        </ListBox.GroupStyle>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

</Grid>


不幸的是.net还没有提供一个“易于使用”的拖放实现。你必须自己做一大堆事情。开始点将是一个
行为
开始拖动,而
ContentControl
是用户可以放置东西的区域。有了这些定义,您就可以轻松地重用这个概念。在以下示例中,
StackPanel
可以“拖动”到
TextBlock
周围的不可见“区域”。通过这种方式,您可以手动对书籍进行排序(将书籍放在鼠标指针的前面/后面)

如果你想把书放在书头上,用
DropArea
环绕它们。您也可以通过这两种方式实现

您的xaml将如下所示:

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
[...]
<ListBox x:Name="lbPersonList" Margin="19,17,162,25" AlternationCount="2" ItemsSource="{Binding}">
    <ListBox.GroupStyle>
        <GroupStyle ContainerStyle="{StaticResource ContainerStyle}"/>
    </ListBox.GroupStyle>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <myOwn:DropArea>
                    <TextBlock Text="{Binding Name}"/>
                </myOwn:DropArea>
                <i:Interaction.Behaviors>
                    <myOwn:DragBehavior/>
                </i:Interaction.Behaviors>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
public class DragBehavior : Behavior<FrameworkElement>
    [...]
    protected override void OnAttached()
    {
        AssociatedObject.MouseMove += AssociatedObject_MouseMove;
        AssociatedObject.MouseDown += AssociatedObject_MouseLeftButtonDown;
        AssociatedObject.MouseLeave += AssociatedObject_MouseLeave;
        base.OnAttached();
    }
    protected override void OnDetaching()
    {
        AssociatedObject.MouseMove -= AssociatedObject_MouseMove;
        AssociatedObject.MouseDown -= AssociatedObject_MouseLeftButtonDown;
        AssociatedObject.MouseLeave -= AssociatedObject_MouseLeave;
        base.OnDetaching();
    }
    protected virtual void AssociatedObject_MouseMove(object sender, MouseEventArgs e)
    {
        if (some condition of mouse button states or mouse moves)
        {
                DataObject data = new DataObject();
                data.SetData(typeof(anyKeyType), anyData);
                data.SetData(typeof(anyOtherKeyType), anyOtherData);
                DragDrop.DoDragDrop(fe, data, DragDropEffects.Move);                
        }
    }
public class DropArea : ContentControl
    [...]
    public DropArea()
    {
        DragEnter += AssociatedObjectDragEnter;
        DragLeave += AssociatedObjectDragLeave;
        DragOver += AssociatedObjectDragOver;
        IsTabStop = false;
        AllowDrop = true;
    }
    protected override void AssociatedObjectDrop(object sender, DragEventArgs e)
    {
        object o = e.Data.GetData(typeof(anyKeyType));
        //handle dropped data
    }
DropArea
将如下所示:

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
[...]
<ListBox x:Name="lbPersonList" Margin="19,17,162,25" AlternationCount="2" ItemsSource="{Binding}">
    <ListBox.GroupStyle>
        <GroupStyle ContainerStyle="{StaticResource ContainerStyle}"/>
    </ListBox.GroupStyle>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <myOwn:DropArea>
                    <TextBlock Text="{Binding Name}"/>
                </myOwn:DropArea>
                <i:Interaction.Behaviors>
                    <myOwn:DragBehavior/>
                </i:Interaction.Behaviors>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
public class DragBehavior : Behavior<FrameworkElement>
    [...]
    protected override void OnAttached()
    {
        AssociatedObject.MouseMove += AssociatedObject_MouseMove;
        AssociatedObject.MouseDown += AssociatedObject_MouseLeftButtonDown;
        AssociatedObject.MouseLeave += AssociatedObject_MouseLeave;
        base.OnAttached();
    }
    protected override void OnDetaching()
    {
        AssociatedObject.MouseMove -= AssociatedObject_MouseMove;
        AssociatedObject.MouseDown -= AssociatedObject_MouseLeftButtonDown;
        AssociatedObject.MouseLeave -= AssociatedObject_MouseLeave;
        base.OnDetaching();
    }
    protected virtual void AssociatedObject_MouseMove(object sender, MouseEventArgs e)
    {
        if (some condition of mouse button states or mouse moves)
        {
                DataObject data = new DataObject();
                data.SetData(typeof(anyKeyType), anyData);
                data.SetData(typeof(anyOtherKeyType), anyOtherData);
                DragDrop.DoDragDrop(fe, data, DragDropEffects.Move);                
        }
    }
public class DropArea : ContentControl
    [...]
    public DropArea()
    {
        DragEnter += AssociatedObjectDragEnter;
        DragLeave += AssociatedObjectDragLeave;
        DragOver += AssociatedObjectDragOver;
        IsTabStop = false;
        AllowDrop = true;
    }
    protected override void AssociatedObjectDrop(object sender, DragEventArgs e)
    {
        object o = e.Data.GetData(typeof(anyKeyType));
        //handle dropped data
    }
希望这能在路上帮助你。可能有任何框架或库可以解决这个问题,但通过这种方式,您可以满足自己的需要。

AMH

首先修改ListviewItem样式。它是包含listbox的每一行(datatemplate实例)的容器。这是一个在行级别管理拖放的好地方(不是一个行控件,DataTemplate中可能有很多)。 在Visual Studio中,选择列表框,单击鼠标右键,编辑其他模板/编辑生成的项目容器(ItemContainerStyle)/编辑副本

在创建的ListBoxItemStyle中,在setter中添加这三个声明:

        <EventSetter Event="ListBoxItem.DragOver" Handler="ListBoxItemDragOver"/>
        <EventSetter Event="ListBoxItem.Drop" Handler="ListBoxItemDrop"/>
        <EventSetter Event="ListBoxItem.PreviewMouseMove" Handler="ListBoxItemPreviewMouseMove"/>
注意,我还在处理程序中的组头上实现了Drop管理。 因此,需要在XAML groupstyle中声明处理程序:

<ListBox.GroupStyle>
    <GroupStyle>
        <GroupStyle.ContainerStyle>
            <Style TargetType="{x:Type GroupItem}">
                <EventSetter Event="GroupItem.Drop" Handler="GroupItemDrop"/>
                <EventSetter Event="GroupItem.PreviewMouseMove" Handler="ListBoxItemPreviewMouseMove"/>

它可以工作,下面是完整的工作代码:


祝你获得尽可能好的代码

@AMH,这不是你问题的答案吗?AMH,你是否希望获得更多/不同的奖励?对周围的任何人来说:如果我演示如何在一个组中的给定位置,而不是在组中,而是在一个随机位置插入删除的元素,会有用吗?这样,最终用户将成为组的主人。