2

Question: In the btnRevert_Click(...) event of the following code, how can we make the RichTextBox editable?

In my WPF .NET5 app, the btnTest_Click(...) event successfully displays the contents of a RichTextBox into a FlowDocumentReader (fdReader). Then the btnRevert_Click(...) event successfully takes the content back from fdReader and displays it back into RichTextBox, but it makes the RichTextBox ReadOnly.

I tested the scenario by entering simple text "This is a test" inside RichTextBox, and noted that the above back and forth scenario works fine except that the btnRevert_Click(...) event makes the text "This is a test" ReadOnly.

MainWindow.xaml:

<Window x:Class="Wpf_RTBFlowDocTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Wpf_RTBFlowDocTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <DockPanel Name="mainPanel">
            <ToolBar Name="mainToolBar" Height="30" DockPanel.Dock="Top">
                <Button x:Name="btnTest" Content="Test" Click="btnTest_Click"/>
                <Button x:Name="btnRevert" Content="Revert" Click="btnRevert_Click"/>
            </ToolBar>
            <RichTextBox Name="rtbTest" AcceptsTab="True"></RichTextBox>
            <FlowDocumentReader x:Name="fdReader" Visibility="Collapsed">
                <FlowDocument>
                    <Paragraph>Text inside paragraph.</Paragraph>
                </FlowDocument>
            </FlowDocumentReader>
        </DockPanel>
    </Grid>
</Window>

MainWindow.xaml.cs:

private void btnTest_Click(object sender, RoutedEventArgs e)
{
    FlowDocument RTBflowDoc = rtbTest.Document;
    rtbTest.Visibility = Visibility.Collapsed;
    rtbTest.Document = new FlowDocument();
    fdReader.Document = RTBflowDoc;
    fdReader.Visibility = Visibility.Visible;
}

private void btnRevert_Click(object sender, RoutedEventArgs e)
{
    FlowDocument fdReaderFlowDoc = fdReader.Document;
    fdReader.Document = new FlowDocument();
    rtbTest.IsReadOnly = false;
    rtbTest.Document = fdReaderFlowDoc;
    rtbTest.Visibility = Visibility.Visible;
}
Jackdaw
  • 7,626
  • 5
  • 15
  • 33
nam
  • 21,967
  • 37
  • 158
  • 332

1 Answers1

2

The FlowDocumentReader provides a control to view a flow content (doesn't support edit).

The following lines in btnRevert_Click() method, from question above, just set rtbTest.Document to fdReader.Document:

FlowDocument fdReaderFlowDoc = fdReader.Document;
fdReader.Document = new FlowDocument();  
rtbTest.Document = fdReaderFlowDoc;

But because of the FlowDocument in the FlowDocumentReader control doesn't support editing, the easiest way to obtain document from the FlowDocumentReader control for editing is to clone it like below:

private void btnRevert_Click(object sender, RoutedEventArgs e)
{   
    var range = new TextRange(fdReader.Document.ContentStart, fdReader.Document.ContentEnd);
    if (!range.IsEmpty)
    {
        using (var stream = new MemoryStream())
        {
            range.Save(stream, DataFormats.XamlPackage);
            var copyto = new TextRange(rtbTest.Document.ContentEnd, rtbTest.Document.ContentEnd);
            copyto.Load(stream, DataFormats.XamlPackage);
        }
    }
    rtbTest.Visibility = Visibility.Visible;
    fdReader.Visibility = Visibility.Collapsed;
}
Jackdaw
  • 7,626
  • 5
  • 15
  • 33
  • Is there a workaround so that when you get the same content back to RichTextBox it also becomes editable as it was originally? – nam May 21 '21 at 22:33
  • @nam: See the updated the `btnRevert_Click()` method above. – Jackdaw May 21 '21 at 22:50
  • Your suggestion worked (thank you) but it kept the formatting of fdReader. Let me see if I can bring the formatting of RichTexBox back, as well. – nam May 21 '21 at 23:03
  • @nam: Of course you will get whole document including formatting! If it's required to get the same document just don't use document from `fdReader`. You have the original document without formatting in the `rtbTest`! – Jackdaw May 21 '21 at 23:09
  • 1
    @nam: BTW in the `btnTest_Click()` method you should clone the `rtbTest.Document` . Don't set `fdReader.Document` directly to the `rtbTest.Document`. I hope this fix will resolve your problem. – Jackdaw May 21 '21 at 23:16
  • Your suggestion of cloning rtbTest.Document instead, worked, as well. – nam May 22 '21 at 15:27
  • I have an issue posted [here](https://stackoverflow.com/q/68476757/1232087) in case you get time to comment etc. – nam Jul 22 '21 at 00:26