I’m creating a chat app in Windows 8 Windows Metro Style App. I need to append the conversation in a richtextblock or textblock in XAML. Would somebody tell me the equivalent for this Code block?
public void AppendConversation(string str)
{
conversation.Append(str);
rtbConversation.Text = conversation.ToString();
rtbConversation.Focus();
rtbConversation.SelectionStart = rtbConversation.Text.Length - 1;
rtbConversation.ScrollToCaret();
rtbSendMessage.Focus();
}
Since WPF uses
System.Windows.Controlsinstead ofSystem.Windows.Forms, we must consider the following1.
System.Windows.Controls.RichTextBoxdoes not have a property forTextto set its value, we may set its value creating a new class ofTextRangesince the control depends onTextPointerwhich can be defined usingTextRange2. Selections in
System.Windows.Controls.RichTextBoxdoes not depend onintyet they are held byTextPointer. So, we can’t saybut we can say
which will do the same as
rtbConversation.SelectionStart = rtbConversation.Text.Length - 1;Remark: You can always retrieve the beginning of the selection in WPF using
RichTextBox.Selection.StartNotice:
RichTextBox.Selection.Startoutputs a class of nameTextPointerbut not a struct of nameint3. Finally,
System.Windows.Controls.RichTextBoxdoes not have a definition forScrollToCaret();. In this case, we may use one of the following voids regarding your controlrtbConversationSo, your void should look like this in WPF
Example
Thanks,
I hope you find this helpful 🙂