参考资料:
WPF中ListBox滚动条自动滚动
WPF ListBox如何实现动态加载数据,并且滚动条自动下拉
1、UI
<Window x:Class="ForTest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:ForTest" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid> <Grid.RowDefinitions> <RowDefinition></RowDefinition> <RowDefinition></RowDefinition> <RowDefinition></RowDefinition> </Grid.RowDefinitions> <ProgressBar Grid.Row="0" Name="progressBar" Height="50" Width="600"></ProgressBar> <Button Grid.Row="1" Height="30" Width="100" Click="Button_Click" Margin="0,0,0,60">开始</Button> <ScrollViewer Grid.Row="2"> <ListBox Name="list" ItemsSource="{Binding Datas,UpdateSourceTrigger=PropertyChanged}"> <ListBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding ElemId,UpdateSourceTrigger=PropertyChanged}"></TextBlock> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </ScrollViewer> </Grid> </Window>
2、UI cs
using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Windows.Threading; namespace ForTest { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window, INotifyPropertyChanged { #region UI更新接口 public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName = null) { if (PropertyChanged != null) PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName)); } #endregion public ObservableCollection<ElementData> Datas { get; set; } public MainWindow() { Datas = new ObservableCollection<ElementData>(); InitializeComponent(); DataContext = this; } private void Button_Click(object sender, RoutedEventArgs e) { new Thread(()=> { for (int i = 0; i < 100; i++) { ElementData elementData = new ElementData(); elementData.ElemId = DateTime.Now.ToLongTimeString(); System.Windows.Application.Current.Dispatcher.Invoke((Action)(() => { Datas.Add(elementData); scroll.ScrollToEnd(); })); Thread.Sleep(300); } }) { IsBackground = true }.Start(); } } public class ElementData : INotifyPropertyChanged { #region UI更新接口 public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName = null) { if (PropertyChanged != null) PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName)); } #endregion #region 属性 /// <summary> /// 元素ID /// </summary> private string elemId; public string ElemId { get { return elemId; } set { elemId = value; OnPropertyChanged("ElemId"); } } #endregion } }