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

后端 未结 6 960
心在旅途
心在旅途 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:47

    Refering to Sam's answer you first have to build your comparer. The following one is also capable of views that do not have a dedicated wish to be positioned at. To attach this comparer to the region that has to be sorted you can use a way intruduced by the prism manual:

    public partial class MainView : UserControl
    {
        public MainView( ) 
        {
            InitializeComponent( );
    
            ObservableObject observableRegion = RegionManager.GetObservableRegion( ContentHost );
    
            observableRegion.PropertyChanged += ( sender, args ) =>
            {
                IRegion region = ( (ObservableObject)sender ).Value;
                region.SortComparison = CompareViews;
            };
        }
    
        private static int CompareViews( object x, object y )
        {
            IPositionView positionX = x as IPositionView;
            IPositionView positionY = y as IPositionView;
            if ( positionX != null && positionY != null )
            {
                //Position is a freely choosable integer
                return Comparer.Default.Compare( positionX.Position, positionY.Position );
            }
            else if ( positionX != null )
            {
                //x is a PositionView, so we favour it here
                return -1;
            }
            else if ( positionY != null )
            {
                //y is a PositionView, so we favour it here
                return 1;
            }
            else
            {
                //both are no PositionViews, so we use string comparison here
                return String.Compare( x.ToString( ), y.ToString( ) );
            }
        }
    }
    

提交回复
热议问题