Prism for Silverlight: How to maintain views in a specific order inside a region

后端 未结 6 942
心在旅途
心在旅途 2020-12-18 12:19

I am creating sort of a \"Navigation panel\" (which is actually an ItemControl) for SL and using Regions to allow each module to add his link to the panel.

Problem i

6条回答
  •  天涯浪人
    2020-12-18 12:46

    At least in prism V4 there you can tell the region manager how to sort the views in a specific region. You just need to provide a compare function to the region.

    This example sorts by a very stupid value, the function name:

    private static int CompareViews(object x, object y)
    {
      return String.Compare(x.ToString(), y.ToString());
    }
    
    this._regionManager.Regions["MyRegion"].SortComparison = CompareViews;
    

    Of course the region needs to be known to the region manager before you can set the SortComparison. So far the only workaround I found to achieve this was to defer to set the comparison function using the Dispatcher:

    private readonly IRegionManager _regionManager;
    
    [ImportingConstructor]
    public ShellViewModel(IRegionManager regionManager)
    {
      this._regionManager = regionManager;
      Dispatcher dp = Dispatcher.CurrentDispatcher;
      dp.BeginInvoke(DispatcherPriority.ApplicationIdle, new ThreadStart(delegate
      {
        if (this._regionManager.Regions.ContainsRegionWithName("MyRegion"))
          this._regionManager.Regions["MyRegion"].SortComparison = CompareViews;
      }));
    }
    

    Of course you should use some more useful information than the class name for the sorting order, but this should be easy to solve.

提交回复
热议问题