Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I found that Sam's solution worked, but discovered that it executes the sort when all views have been added to the region, thus sorting the views twice.</p> <p>Although it is still a valid solution, reading <a href="http://compositewpf.codeplex.com/discussions/250585" rel="nofollow">this</a> post in Prism discussion made me think about a way of implementing this just when the region has been loaded, but before any views have been added yet.</p> <p><strong>1 - Subscribe to the CollectionChanged of Regions collection</strong></p> <p>I placed this in the Shell ViewModel code which is the one associated to the View that contains the region I want to sort. Whenever the IRegionManager import has been resolved I subscribe to the CollectionChanged event of its Regions collection:</p> <pre><code>this._regionManager.Regions.CollectionChanged += new NotifyCollectionChangedEventHandler(Regions_CollectionChanged); </code></pre> <p><strong>2 - Change the SortComparison of the region in the event delegate</strong></p> <p>Then the delegate <code>Regions_CollectionChanged</code> will execute whenever the Regions collection is updated and will change the <code>SortComparison</code> of my desired region:</p> <pre><code>void Regions_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { if (e.Action == NotifyCollectionChangedAction.Add) { foreach (var o in e.NewItems) { IRegion region = o as IRegion; if (region != null &amp;&amp; region.Name == RegionNames.NavigationRegion) { region.SortComparison = CompareNavigatorViews; } } } } </code></pre> <p><strong>3 - Define the CompareNavigatorViews delegate</strong></p> <p>In my case, I just sort the views by the title of the assembly where they are contained, you can implement your own compare method here. Remember that the objects you'll receive here are the Views and not the ViewModels.</p> <pre><code>private static int CompareNavigatorViews(object x, object y) { if (x == null) if (y == null) return 0; else return -1; else if (y == null) return 1; else { AssemblyInfo xAssemblyInfo = new AssemblyInfo(Assembly.GetAssembly(x.GetType())); AssemblyInfo yAssemblyInfo = new AssemblyInfo(Assembly.GetAssembly(y.GetType())); return String.Compare(xAssemblyInfo.Title, yAssemblyInfo.Title); } } </code></pre> <p>Just in case somebody asks, the AssemblyInfo class is an utility class I made. To get the title of an assembly you could use this function:</p> <pre><code>string GetAssemblyTitle(Assembly assembly) { object[] attributes = assembly.GetCustomAttributes(typeof(AssemblyTitleAttribute), false); if (attributes.Length == 1) { return (attributes[0] as AssemblyTitleAttribute).Title; } else { // Return the assembly name if there is no title return this.GetType().Assembly.GetName().Name; } } </code></pre> <p>Hope this helps someone!</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. This table or related slice is empty.
    1. This table or related slice is empty.
    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