【问题标题】:Add View to the top of ItemsControl region将 View 添加到 ItemsControl 区域的顶部
【发布时间】:2012-12-20 15:33:02
【问题描述】:

我在复合 Prism MVVM 应用程序中有一个 ItemsControl 区域

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

现在我想在该区域显示我的 NotificationViews,如下所示:

我只是导航到视图并将它们添加到我的 ItemsControl 区域。但问题是新视图总是添加到底部。我真的希望新视图显示在顶部。有没有办法实现这一目标?非常感谢您。

【问题讨论】:

    标签: wpf mvvm prism itemscontrol


    【解决方案1】:

    我想你会发现这会有所帮助:Sorting views in Prism/MEF

    基本上:

    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( ) );
            }
        }
    

    您会注意到该区域有一个 SortComparison 属性。您只需为首先排序最新视图的区域创建自定义 SortComparison。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多