Question: How can we create a FlowDocumentReader from the content of a RichTextBox?
I know we cannot assign the same logical child of a control to another control; nor can we assign null to the Document property of a RichTextBox. Hence the following code attempt fails. Are there workarounds or better solutions?
MainWindow.xaml:
<Window x:Class="WPFRichTextBxTest.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:WPFRichTextBxTest"
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"/>
</ToolBar>
<RichTextBox Name="rtbTest" AcceptsTab="True"/>
<FlowDocumentReader x:Name="fdReader"></FlowDocumentReader>
</DockPanel>
</Grid>
</Window>
Code:
private void btnTest_Click(object sender, RoutedEventArgs e)
{
FlowDocument flowDoc = rtbTest.Document;
rtbTest.Visibility = Visibility.Collapsed;
rtbTest.Document = null;
fdReader.Document = flowDoc ;
}
You could set the Document
property to a new empty FlowDocument
:
private void btnTest_Click(object sender, RoutedEventArgs e)
{
FlowDocument flowDoc = rtbTest.Document;
rtbTest.Visibility = Visibility.Collapsed;
rtbTest.Document = new FlowDocument(); //<--
fdReader.Document = flowDoc;
}