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

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

问题描述

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

解决方案

您无法对 GridView ,除非 ItemsSource 绑定到 ObservableCollection 并且 CanReorderItems CanDragItems AllowDrop 设置为 true 。使用 CollectionViewSource 来启用 gridview 中的重新排序是 。实际上,通常使用 collectionviewsource 来分组 gridview ,并且在数据分组时不能重新排序。



无论如何,您的XAML应该是这样的:

 < Grid Background = 黑 > 
< Grid.DataContext>
< local:MyModel />
< /Grid.DataContext>
< GridView CanReorderItems =TrueCanDragItems =TrueAllowDrop =True
ItemsSource ={Binding Items}>
< / GridView>
< / Grid>

虽然任何可枚举都可以绑定到一个 GridView 的I​​temsSource 它只是一个 ObservableCollection ,它启用重新排序。是的,你可以使用实现重新排序的自定义类型,但是为什么在 ObservableCollection 为你做这件事时会混淆?



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

  public class MyModel 
{
public MyModel
{
foreach(Enumerable.Range(1,50)中的var item)
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 Item_CollectionChanged(object sender,NotifyCollectionChangedEventArgs e)
{
switch(e.Action)
{
case NotifyCollectionChangedAction.Remove:
m_ReorderItem = e。 OldItems [0];
m_ReorderIndexFrom = e.OldStartingIndex;
休息;
case NotifyCollectionChangedAction.Add:
if(m_ReorderItem == null)
return;
var _ReorderIndexTo = e.NewStartingIndex;
HandleReorder(m_ReorderItem,m_ReorderIndexFrom,_ReorderIndexTo);
m_ReorderItem = null;
休息;



void HandleReorder(object item,int indexFrom,int indexTo)
{
Debug.WriteLine(Reorder:{0},从:{1},到:{2},item,indexFrom,indexTo);






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



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



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


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

哦,回答你的问题!

没有事件表明重新排序。重新排序是基于底层 ObservableCollection CollectionChanged 事件中操作组合的派生操作。顺便说一下,如果您选择:
>

顺便说一下,这里是绑定到 CollectionViewSource 的示例语法

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

祝您好运


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!

解决方案

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.

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>

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

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.

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.

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.

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

Oh, to answer your question!

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?

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>

Best of luck.

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

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