textBox經常用來顯示程序的運行狀態或者消息,如何讓他自動滾動呢?
在顯示消息代碼下加一條自動滾動到底部的語句即可:
TextBox1.ScrollToEnd();
(如果要顯示垂直滾動條設置VerticalScrollBarVisibility="Auto",如果不顯示設置為Hidden)
我用的程序代碼如下:
this.Dispatcher.Invoke(new Action(() => { //大於100行清除記錄,可選 if (txtReceived.LineCount > 100) { txtReceived.Clear(); } txtReceived.AppendText(string.Format("當前時間{0:HH:MM:ss}\n", DateTime.Now)); //自動滾動到底部 txtReceived.ScrollToEnd(); }));