I have 2 RichTextBoxes (rtb1, rtb2), I something wrote in rtb1 and click on enter key, on this event is added text from rtb1 to rtb2. I solved this in code behind, it is possible this same write in XAML?
C# code:
private void rtb2_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
var textElement = new Run() { Text = rtb2.Text };
var paragraph = new Paragraph();
paragraph.Inlines.Add(textElement);
rtb1.Document.Blocks.Add(paragraph);
rtb2.Document.Blocks.Clear();
//On this place I would like set start position for input text in rtb2 richtextbox on the start position
}
}
Thank for your advances.
If you really need your combined
RichTextBoxto have generalized document editing capabilities, what you are doing is probably the easiest and no, there is no pure-XAML way to do it.On the other hand, if you are writing a chat application or something like it there may be a much better way to do it: Replace your combined
RichTextBoxwith anItemsControlthat displays all the chat items as individual items. Then when Enter is pressed, add the text to a collection in your model. As long as the collection implementsINotifyCollectionChanged, the new text will appear in yourItemsControl. And if the template you use for yourItemsControlallows editing, your user will be able to edit the item.Which way you go all depends on what you’re trying to accomplish.