Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You cannot reorder a <code>GridView</code> unless the <code>ItemsSource</code> is bound to an <code>ObservableCollection</code> and <code>CanReorderItems</code>, <code>CanDragItems</code>, and <code>AllowDrop</code> are set to <code>true</code>. It is <strong>not</strong> necessary to use a <code>CollectionViewSource</code> to enable reordering in your <code>gridview</code>. In fact, a <code>collectionviewsource</code> is often used for grouping a <code>gridview</code> and reordering is not possible when data is grouped.</p> <p>Anyway, your XAML would look like this:</p> <pre><code>&lt;Grid Background="Black"&gt; &lt;Grid.DataContext&gt; &lt;local:MyModel/&gt; &lt;/Grid.DataContext&gt; &lt;GridView CanReorderItems="True" CanDragItems="True" AllowDrop="True" ItemsSource="{Binding Items}"&gt; &lt;/GridView&gt; &lt;/Grid&gt; </code></pre> <p>Although any <code>enumerable</code> can be bound to the <code>ItemsSource</code> of a <code>GridView</code> it is only an <code>ObservableCollection</code> that enables reorder. Yes, you can use a custom type that implements reorder, but why mess with that when <code>ObservableCollection</code> does it for you?</p> <p>Your view model might look like this:</p> <pre><code>public class MyModel { public MyModel() { foreach (var item in Enumerable.Range(1, 50)) Items.Add(item); Items.CollectionChanged += Items_CollectionChanged; } ObservableCollection&lt;int&gt; m_Items = new ObservableCollection&lt;int&gt;(); public ObservableCollection&lt;int&gt; 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); } } </code></pre> <p>In the code above, the reorder <code>event</code> is not real. It is derived from a combination of the "Remove" action and the "Add" action in the <code>CollectionChanged</code> event. Here's why this is <strong>awesome</strong>. If the reorder was only available from the <code>GridView</code> then the <code>ViewModel</code> would not be able to handle it. Because the underlying list is how you detect reorder, the <code>ViewModel</code> is enabled.</p> <p>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.</p> <p>Remember, <code>GridView</code> is not the only control that allows reorder. Any control based on <code>ListViewBase</code> (like the <code>ListView</code>) supports reorder - still using <code>ObservableCollection</code>. But <code>GridView</code> is the most common control to use this feature. For sure.</p> <blockquote> <p>Reference: <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.listviewbase.canreorderitems">http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.listviewbase.canreorderitems</a></p> </blockquote> <p><strong>Oh, to answer your question!</strong></p> <p>There is no event that indicates a reorder. Reorder is a derived action based on a combination of actions in the underlying <code>ObservableCollection</code> <code>CollectionChanged</code> event. Make sense?</p> <p>By the way, here's sample syntax to bind to a <code>CollectionViewSource</code>, if you choose to:</p> <pre><code>&lt;Grid Background="Black"&gt; &lt;Grid.DataContext&gt; &lt;local:MyModel/&gt; &lt;/Grid.DataContext&gt; &lt;Grid.Resources&gt; &lt;CollectionViewSource x:Name="CVS" Source="{Binding Items}" /&gt; &lt;/Grid.Resources&gt; &lt;GridView CanReorderItems="True" CanDragItems="True" AllowDrop="True" ItemsSource="{Binding Source={StaticResource CVS}}" &gt; &lt;/GridView&gt; &lt;/Grid&gt; </code></pre> <p>Best of luck.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload