MVVM下listbox默认显示最后一行

原文: MVVM下listbox默认显示最后一行

原文地址:http://stackoverflow.com/questions/16866309/listbox-scroll-into-view-with-mvvm

public class ScrollingListBox : ListBox
    {
        protected override void OnItemsChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems!=null)//此处需要判空
            {
                int newItemCount = e.NewItems.Count;

                if (newItemCount > 0)
                    this.ScrollIntoView(e.NewItems[newItemCount - 1]);

                base.OnItemsChanged(e);
            }            
        }
    }
<controls:ScrollingListBox ItemsSource="{Binding ChatList}" Background="WhiteSmoke"  Style="{StaticResource ChatListBoxStyle}"/>

猜你喜欢

转载自www.cnblogs.com/lonelyxmas/p/10789479.html