Hi guys,
Just want to select a row inside a datagrid and display one rtf format field content inside a richtexbox.
First I did:
<RichTextBox x:Name="richTextBox1" HorizontalAlignment="Left" Height="268" Margin="202,41,0,0" VerticalAlignment="Top" Width="589"><FlowDocument><Paragraph><Run Text="{Binding ElementName=dataGridView1, Path=SelectedItem.Texto}"/></Paragraph></FlowDocument></RichTextBox>
It was ok, but the text format was plaint text, not rtf. So, searching the foruns, I found a method to convert plain text to rtf. Then I did:
private void dataGridView1_SelectionChanged(object sender, SelectionChangedEventArgs e) { TextRange textRange = new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd); string text = textRange.Text; SetRTFText(text); } public void SetRTFText(string text) { richTextBox1.SelectAll(); MemoryStream stream = new MemoryStream(ASCIIEncoding.Default.GetBytes(text)); richTextBox1.Selection.Load(stream, DataFormats.Rtf); }
The first time the selection of datagrid changes, it does everything ok. But when I change the selection for the second time the program tries to convert the same text that was already converted previously. It seems it looses the binding. What am I doing wrong?