我在Visual Studios 2012中制作了我的第一个Windows应用商店应用程序。我有一个gridview控件,可以重新排序。当列表重新排序时,我需要运行代码。我已经尝试了Drop事件。它不会着火。我尝试了其他几个拖动事件,这些事件也没有启动。看起来这应该是如此简单......感谢您的时间!XAML/C#:重新排序gridview后会发生什么事件?
回答
除非ItemsSource
绑定到ObservableCollection
和CanReorderItems
,CanDragItems
,并AllowDrop
设置为true
不能重新排序GridView
。它是而不是必需使用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>
虽然任何enumerable
可以绑定到一个GridView
它只是一个ObservableCollection
,使订货的ItemsSource
。是的,你可以使用自定义类型来实现重新排序,但为什么ObservableCollection
会为你做什么?
您的视图模型可能是这样的:
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
。
每种情况都有所不同。你可能不关心索引,所以你可以简化代码。您可能不允许添加或从集合中删除,因此您只需要监视添加操作。再次,这取决于你的情况。我上面的示例代码应该会处理99%的案例。
请记住,GridView
不是唯一允许重新排序的控件。任何基于ListViewBase
(如ListView
)的控件都支持重新排序 - 仍然使用ObservableCollection
。但GridView
是使用此功能的最常用控件。当然。
哦,要回答你的问题!
没有事件表示重新排序。重新排序是基于底层ObservableCollection
CollectionChanged
事件中的操作组合导出的操作。合理?
顺便说一句,这里的样本语法来绑定到CollectionViewSource
,如果你选择:
<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>
好运。
非常感谢你的解释!很有帮助!! – 2013-05-14 12:51:20
一直在寻找这一整天的答案 - 这是完美的,正是我所需要的。 – 2013-10-03 02:23:00
@ JerryNixon-MSFT Inside Items_CollectionChanged改变了事件,我将如何处理__adding__到集合?由于目前__removing__跟着__adding__会导致'HandleReorder()' – lbrahim 2014-12-04 15:19:21
- 1. Visual Studio 2008在发布后事件之后会发生什么事件?
- 2. 为什么LostFocus事件不会发生?
- 3. GridView排序事件未触发
- 4. Form.Close后会发生什么?
- 5. EntityManager.persist()后会发生什么
- 6. netdev_open后会发生什么?
- 7. * ngFor后会发生什么?
- 8. 如果我在推后重新绑定会发生什么?
- 9. C#多线程和重新排序 - 这里发生了什么?
- 10. 在Codefluent中加载后会发生什么事件?
- 11. 表单初始加载后会发生什么事件?
- 12. 在检查点之后写入的事件会发生什么?
- 13. Asp.net GridView - 为什么DataRowBound在排序上发生改变?
- 14. GridView的排序事件
- 15. 为什么在CQRS中发生事件后更新AggreateRoot
- 16. Flex图表刷新后会触发什么事件
- 17. C#GridView/RadioButton不会触发事件...为什么?
- 18. 为什么我的GridView的SelectedIndexChanged事件不会触发?
- 19. 在asp.net Gridview控件中触发事件的顺序是什么?
- 20. iOS在后台发生什么事情时会显示通知?
- 21. 会发生什么?
- 22. 会发生什么?
- 23. 会发生什么?
- 24. Android - 后台活动会发生什么?
- 25. onClick完成后会发生什么。 Android
- 26. 返回ActionResult后会发生什么?
- 27. 我调用MPI_Init()后会发生什么?
- 28. 使用free(),稍后会发生什么?
- 29. pthread_create之后pthread_t会发生什么?
- 30. 执行后TimerTask会发生什么?
可以使用的ObservableCollection的''CollectionChanged'事件',如果您提供'和'的ObservableCollection GridView'的'ItemsSource' ' –
Xyroid
2013-05-06 05:10:56