我的程序會跟蹤文本框中的字符和行數。我正在嘗試創建一個狀態欄,向用戶顯示此信息。問題是當狀態欄更新時,它不會取代舊信息,而只是將其添加到行中。如何更新狀態欄?
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
int numChars = TextBox.Text.Length;
int numLines = TextBox.LineCount;
TextBlock statusText = new TextBlock();
statusText.Text = "Line " + (numLines) + ", Char " + (numChars);
Status.Items.Add(statusText);
}
也許是因爲你「添加'它的狀態欄,而不是'清除'它?代碼沒有說明舊的項目應該被清除或刪除,所以他們自然停留。 – CShark
@CShark啊,這是有道理的。謝謝! – asa
@asa爲什麼要手動添加TextBlock?在xaml中添加TextBlock並更新代碼中的Text會容易得多。 – Valerii