0
我正在爲控制檯應用程序創建GUI並希望輸出顯示在ListBox中。 如果我在文本框中鍵入「時間」並點擊「發送」按鈕,該命令將被髮送到控制檯應用程序。問題是當發送命令時,列表框會自動停止向下滾動。我是否做了不正確的事情或者有其他方式應該處理自動滾動?WPF ListBox AutoScroll停止工作
當應用程序加載它的應用程序加載滾動停止工作
申請後經過滾動正確
獲取命令,它增加了一個項目的列表框導致AutoScroll方法運行,但即使代碼執行列表框不向下滾動。我試過在發送命令後添加一個對AutoScroll的調用,但這也不起作用。
private void btnStart_Click(object sender, RoutedEventArgs e)
{
server = new Server();
string ExecPath = @"Server.exe";
string ConfPath = @"serverconfig.txt";
//Starts the process and passes the config cmd-line argument
//redirects output and input, and prevents a cmd window from being created
server.Start(ExecPath, ConfPath);
server.SrvProcess.OutputDataReceived += SrvProcess_OutputDataReceived;
server.SrvProcess.BeginOutputReadLine();
}
private void btnSend_Click(object sender, RoutedEventArgs e)
{
int itemCount = lstOutput.Items.Count;
if (!String.IsNullOrEmpty(txtCmd.Text))
{
if (lstOutput.Items[itemCount - 1].ToString() != String.Empty)
lstOutput.Items.Add(String.Empty);
lstOutput.Items.Add(": " + txtCmd.Text);
server.SrvProcess.StandardInput.WriteLine(txtCmd.Text);
}
}
void SrvProcess_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e)
{
if (!closing)
{
lstOutput.Dispatcher.Invoke(() => AddItem(e.Data));
lstOutput.Dispatcher.InvokeAsync(() => AutoScroll());
}
}
void AddItem(string Data)
{
lstOutput.Items.Add(Data);
if (Data == "Server started")
{
lstOutput.Items.Clear();
}
}
void AutoScroll()
{
int itemCount = lstOutput.Items.Count - 1;
if (itemCount > -1)
lstOutput.ScrollIntoView(lstOutput.Items[itemCount]);
}