Kapitán Mlíko
Kapitán Mlíko

Reputation: 4538

Add View to the top of ItemsControl region

I have a ItemsControl region in composite Prism MVVM application

<ItemsControl Regions:RegionManager.RegionName="NotificationRegion" AllowDrop="True" ClipToBounds="True" HorizontalAlignment="Right" Margin="0,40,20,20" Width="280" />

And now I want to display my NotificationViews in that region like this: enter image description here

I simply navigate to views and they are added to my ItemsControl region. But problem is that new View is always added to the bottom. I really want new views to be displayed at the top. Is there any way to achieve this? Thank you very much in advance.

Upvotes: 1

Views: 728

Answers (1)

Faster Solutions
Faster Solutions

Reputation: 7005

I think you'll find this helps: Sorting views in Prism/MEF

Basically:

public MainView( ) 
    {
        InitializeComponent( );

        ObservableObject<IRegion> observableRegion = RegionManager.GetObservableRegion( ContentHost );

        observableRegion.PropertyChanged += ( sender, args ) =>
        {
            IRegion region = ( (ObservableObject<IRegion>)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<int>.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( ) );
        }
    }

You'll notice that the region has a SortComparison property. You just need to create a custom SortComparison for the region which orders your newest views first.

Upvotes: 1

Related Questions