由于 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;
备注:您始终可以使用RichTextBox.Selection.Start
通知在 WPF 中检索选择的开头:RichTextBox.Selection.Start输出名称类TextPointer而不是名称结构int
3. 最后,System.Windows.Controls.RichTextBox没有 的定义ScrollToCaret();。在这种情况下,我们可能会使用以下关于您的控制权的空白之一rtbConversation
rtbConversation.ScrollToEnd();
rtbConversation.ScrollToHome();
rtbConversation.ScrollToHorizontalOffset(double offset);
rtbConversation.ScrollToVerticalOffset(double offset);
所以,你的 void 在 WPF 中应该是这样的
例子
public void AppendConversation(string str)
{
conversation.Append(str) // Sorry, I was unable to detect the type of 'conversation'
new TextRange(rtbConversation.Document.ContentStart,
rtbConversation.Document.ContentEnd).Text =
conversation.ToString();
rtbConversation.Focus();
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.ScrollToEnd();
rtbSendMessage.Focus();
}
谢谢,
我希望你觉得这有帮助:)