XAML/C#:重新排序 gridview 后会触发什么事件? [英] XAML/C#: What event fires after reordering a gridview?

查看:30
本文介绍了XAML/C#:重新排序 gridview 后会触发什么事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Visual Studios 2012 中制作我的第一个 Windows 应用商店应用程序.我有一个 gridview 控件,我可以重新排序.我有在重新排序列表时需要运行的代码.我已经尝试过 Drop 事件.它不会着火.我尝试了其他几个拖动事件,但也没有触发.看起来这应该很简单......感谢您的时间!

I am making my first Windows Store app in Visual Studios 2012. I have a gridview control that I have enabled to be reordered. I have code that I need to run when the list is reordered. I have tried the Drop event. It does not fire. I tried several other drag events, which also did not fire. It seems like this should be so simple... Thanks for your time!

推荐答案

除非 ItemsSource 绑定到 ObservableCollection,否则您不能重新排序 GridView> 和 CanReorderItemsCanDragItemsAllowDrop 设置为 true.不必使用 CollectionViewSourcegridview 中启用重新排序.实际上,collectionviewsource 经常用于对gridview 进行分组,并且在分组数据时无法重新排序.

You cannot reorder a GridView unless the ItemsSource is bound to an ObservableCollection and CanReorderItems, CanDragItems, and AllowDrop are set to true. It is not necessary to use a CollectionViewSource to enable reordering in your gridview. In fact, a collectionviewsource is often used for grouping a gridview and reordering is not possible when data is grouped.

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

Anyway, your XAML would look like this:

<Grid Background="Black">
    <Grid.DataContext>
        <local:MyModel/>
    </Grid.DataContext>
    <GridView CanReorderItems="True" CanDragItems="True" AllowDrop="True"
              ItemsSource="{Binding Items}">
    </GridView>
</Grid>

尽管任何 enumerable 都可以绑定到 GridViewItemsSource,但只有一个 ObservableCollection 可以启用重新排序.是的,您可以使用实现重新排序的自定义类型,但是当 ObservableCollection 为您执行此操作时,为什么要搞砸呢?

Although any enumerable can be bound to the ItemsSource of a GridView it is only an ObservableCollection that enables reorder. Yes, you can use a custom type that implements reorder, but why mess with that when ObservableCollection does it for you?

您的视图模型可能如下所示:

Your view model might look like this:

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);
    }
}

在上面的代码中,重新排序 event 不是真实的.它派生自 CollectionChanged 事件中的移除"操作和添加"操作的组合.这就是为什么这很棒.如果重新排序只能从 GridView 获得,那么 ViewModel 将无法处理它.由于底层列表是您检测重新排序的方式,因此启用了 ViewModel.

In the code above, the reorder event is not real. It is derived from a combination of the "Remove" action and the "Add" action in the CollectionChanged event. Here's why this is awesome. If the reorder was only available from the GridView then the ViewModel would not be able to handle it. Because the underlying list is how you detect reorder, the ViewModel is enabled.

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

Every case is slightly different. You may not care about the Index so you can simplify the code. You may not allow adding or removing from the collection so you only need to monitor the Add action. Again, it depends on your situation. My sample code above should get 99% of the cases taken care of.

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

Remember, GridView is not the only control that allows reorder. Any control based on ListViewBase (like the ListView) supports reorder - still using ObservableCollection. But GridView is the most common control to use this feature. For sure.

参考:http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.listviewbase.canreorderitems

哦,回答你的问题!

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

There is no event that indicates a reorder. Reorder is a derived action based on a combination of actions in the underlying ObservableCollection CollectionChanged event. Make sense?

顺便说一下,这里是绑定到 CollectionViewSource 的示例语法,如果您选择:

By the way, here's sample syntax to bind to a CollectionViewSource, if you choose to:

<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>

祝你好运.

这篇关于XAML/C#:重新排序 gridview 后会触发什么事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆