XAML/C#:对gridview重新排序后激发什么事件?

XAML/C#:对gridview重新排序后激发什么事件?,xaml,gridview,windows-store-apps,winrt-xaml,Xaml,Gridview,Windows Store Apps,Winrt Xaml,我正在Visual Studio 2012中制作我的第一个Windows应用商店应用程序。我有一个gridview控件,可以对其进行重新排序。我有一些代码需要在列表重新排序时运行。我试过投球。它不会开火。我尝试了其他几个拖动事件,但也没有触发。看起来这应该很简单。。。谢谢你的时间 除非项资源绑定到可观察集合,并且可以重新排序项,CanDragItems和AllowDrop设置为真,否则无法对网格视图进行重新排序。无需使用CollectionViewSource在gridview中启用重新排序。事

我正在Visual Studio 2012中制作我的第一个Windows应用商店应用程序。我有一个gridview控件,可以对其进行重新排序。我有一些代码需要在列表重新排序时运行。我试过投球。它不会开火。我尝试了其他几个拖动事件,但也没有触发。看起来这应该很简单。。。谢谢你的时间

除非
项资源
绑定到
可观察集合
,并且
可以重新排序项
CanDragItems
AllowDrop
设置为
,否则无法对
网格视图
进行重新排序。无需使用
CollectionViewSource
gridview
中启用重新排序。事实上,
collectionviewsource
通常用于对
gridview
进行分组,并且在分组数据时无法重新排序

无论如何,您的XAML将如下所示:

<Grid Background="Black">
    <Grid.DataContext>
        <local:MyModel/>
    </Grid.DataContext>
    <GridView CanReorderItems="True" CanDragItems="True" AllowDrop="True"
              ItemsSource="{Binding Items}">
    </GridView>
</Grid>
public class MyModel
{
    public MyModel()
    {
        foreach (var item in Enumerable.Range(1, 50))
            Items.Add(item);
        Items.CollectionChanged += Items_CollectionChanged;
    }

    ObservableCollection<int> m_Items = new ObservableCollection<int>();
    public ObservableCollection<int> Items { get { return m_Items; } }

    object m_ReorderItem;
    int m_ReorderIndexFrom;
    void Items_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        switch (e.Action)
        {
            case NotifyCollectionChangedAction.Remove:
                m_ReorderItem = e.OldItems[0];
                m_ReorderIndexFrom = e.OldStartingIndex;
                break;
            case NotifyCollectionChangedAction.Add:
                if (m_ReorderItem == null)
                    return;
                var _ReorderIndexTo = e.NewStartingIndex;
                HandleReorder(m_ReorderItem, m_ReorderIndexFrom, _ReorderIndexTo);
                m_ReorderItem = null;
                break;
        }
    }

    void HandleReorder(object item, int indexFrom, int indexTo)
    {
        Debug.WriteLine("Reorder: {0}, From: {1}, To: {2}", item, indexFrom, indexTo);
    }
}
在上面的代码中,重新排序
事件
不是真实的。它是由
CollectionChanged
事件中的“删除”操作和“添加”操作组合而成。这就是为什么非常棒的原因。如果重新排序只能从
GridView
中获得,那么
ViewModel
将无法处理它。由于基础列表是检测重新排序的方式,因此启用了
ViewModel

每种情况都略有不同。您可能不关心索引,因此可以简化代码。您可能不允许在集合中添加或删除,因此只需监视添加操作。同样,这取决于你的情况。我上面的示例代码应该可以处理99%的案例

请记住,
GridView
不是唯一允许重新排序的控件。任何基于
ListViewBase
(如
ListView
)的控件都支持重新排序-仍然使用
ObservableCollection
。但是
GridView
是使用此功能最常用的控件。当然

参考:

哦,回答你的问题

没有指示重新排序的事件。重新排序是基于基础
ObservableCollection
CollectionChanged
事件中的操作组合的派生操作。有道理吗

顺便说一下,如果您选择:

<Grid Background="Black">
    <Grid.DataContext>
        <local:MyModel/>
    </Grid.DataContext>
    <Grid.Resources>
        <CollectionViewSource x:Name="CVS" Source="{Binding Items}" />
    </Grid.Resources>
    <GridView CanReorderItems="True" CanDragItems="True" AllowDrop="True"
              ItemsSource="{Binding Source={StaticResource CVS}}" >
    </GridView>
</Grid>


祝您好运。

如果您提供了
网格视图
项目资源
,您可以使用
CollectionChanged
事件
observedcollection
,非常感谢您的解释!非常有用!!整天都在寻找答案-这很完美,正是我所需要的。@JerryNixon MSFT Inside
Items\u CollectionChanged
changed事件,我如何处理添加到集合中的问题?因为当前删除后续添加将导致
handleroorder()
,因为您只能通过代码进行添加,所以您实际上不需要倾听您的收藏,只需在添加项目的地方扩展您的逻辑即可。这是一种很好的方法,但我对此有一些问题。我使用它来操作一些GridViewItems,这个方法确实有效,但是我保存了ObservableCollection中的整个集合供以后使用,因此用户不会一次又一次地加载这些项。我的问题是,“CollectionChanged”事件会在添加/删除的每个项目上触发,我希望有一种方法触发我的方法,在添加/删除所有元素后将集合保存到存储中。有办法吗?