WPF はSystem.Windows.Controlsの代わりに使用するためSystem.Windows.Forms、次の点を考慮する必要があります。
1. にはその値を設定System.Windows.Controls.RichTextBoxするためのプロパティがありません。Textその値を設定して新しいクラスを作成することができます。これはTextRange、コントロールが依存するためです。TextPointerTextRange
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);
したがって、ボイドは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();
}
ありがとう、
これがお役に立てば幸いです:)