由於WPF使用System.Windows.Controls
代替System.Windows.Forms
,我們必須考慮以下
1. System.Windows.Controls.RichTextBox
不具有財產Text
將其值設置,我們可以將其值設置創建一個新類的TextRange
由於控制取決於TextPointer
這可以使用被限定TextRange
string _Text = ""
new TextRange(rtbConversation.Document.ContentStart, rtbConversation.Document.ContentEnd).Text = _Text;
2. System.Windows.Controls.RichTextBox
中的選擇不取決於int
,但它們被TextPointer
持有。所以,我們不能說
rtbConversation.SelectionStart = rtbConversation.Text.Length - 1;
,但我們可以說
int TextLength = new TextRange(rtbConversation.Document.ContentStart, rtbConversation.Document.ContentEnd).Text.Length;
TextPointer tr = rtbConversation.Document.ContentStart.GetPositionAtOffset(TextLength - 1, LogicalDirection.Forward);
rtbConversation.Selection.Select(tr, tr);
這將做同樣的rtbConversation.SelectionStart = rtbConversation.Text.Length - 1;
備註:您可以隨時檢索選擇的開始WPF使用RichTextBox.Selection.Start
通知:RichTextBox.Selection.Start
輸出一類名稱TextPointer
但不名int
3的結構最後,System.Windows.Controls.RichTextBox
沒有一個定義ScrollToCaret();
。在這種情況下,我們可以使用關於你的控制rtbConversation
rtbConversation.ScrollToEnd();
rtbConversation.ScrollToHome();
rtbConversation.ScrollToHorizontalOffset(double offset);
rtbConversation.ScrollToVerticalOffset(double offset);
所以,你的虛空應該是這樣的WPF
例
public void AppendConversation(string str)
{
conversation.Append(str) // Sorry, I was unable to detect the type of 'conversation'
#region rtbConversation.Text = conversation.ToString();
// rtbConversation.Text = conversation.ToString();
new TextRange(rtbConversation.Document.ContentStart, rtbConversation.Document.ContentEnd).Text = conversation.ToString();
// rtbConversation.Text = conversation.ToString();
#endregion
#region rtbConversation.Focus();
// rtbConversation.Focus();
rtbConversation.Focus();
// rtbConversation.Focus();
#endregion
#region rtbConversation.SelectionStart = rtbConversation.Text.Length - 1;
// rtbConversation.SelectionStart = rtbConversation.Text.Length - 1;
int TextLength = new TextRange(rtbConversation.Document.ContentStart, rtbConversation.Document.ContentEnd).Text.Length;
TextPointer tr = rtbConversation.Document.ContentStart.GetPositionAtOffset(TextLength - 1, LogicalDirection.Forward);
rtbConversation.Selection.Select(tr, tr);
// rtbConversation.SelectionStart = rtbConversation.Text.Length - 1;
#endregion
#region rtbConversation.ScrollToCaret();
// rtbConversation.ScrollToCaret();
rtbConversation.ScrollToEnd();
// rtbConversation.ScrollToCaret();
#endregion
#region rtbSendMessage.Focus();
//rtbSendMessage.Focus();
rtbSendMessage.Focus();
//rtbSendMessage.Focus();
#endregion
}
下列空間之一謝謝,
我希望你覺得這有幫助:)
非常感謝! – ggsmartboy
@ggsmartboy完全沒有問題。我很高興能夠提供幫助。 [請注意,您可以將帖子標記爲表示您已解決問題的答案](http://i.stack.imgur.com/uqJeW.png)。有一個美好的一天:) –
再次感謝。 。 。祝你有個美好的一天。 。 。 – ggsmartboy