I have a winform in C# .Net 4.6 which has an element host. This hosts a usercontrol containing a WPF RichTextBox. I want to get the text from the RichTextBox to use elsewhere on the winform/application. I would like to use it as a string.
I have tried this;
var elementHost = this.elementHost1;
var wpfTextBox = (System.Windows.Controls.RichTextBox)elementHost.Child;
string richText = new TextRange(wpfTextBox.Document.ContentStart, wpfTextBox.Document.ContentEnd).Text;
textBox1.Text = richText.ToString();
It fails on line 2 with this error;
'Unable to cast object of type 'SyncfusionWindowsFormsApplication3.UserControl1' to type 'System.Windows.Controls.RichTextBox'.'
How do I get the text from the RichtextBox within UserControl1 hosted in elementHost1?
UPDATE - XAML for the usercontrol containing the RichtextBox;
<UserControl x:Class="SyncfusionWindowsFormsApplication3.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup- compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:SyncfusionWindowsFormsApplication3"
mc:Ignorable="d" Width="1093.598" Height="423.11">
<RichTextBox HorizontalAlignment="Left" Height="404" Margin="14,9,0,0" VerticalAlignment="Top" Width="1070" FontFamily="Arial" FontSize="16" SpellCheck.IsEnabled="True" BorderBrush="Black" BorderThickness="3" ScrollViewer.CanContentScroll="True" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" TextChanged="RichTextBox_TextChanged">
<FlowDocument>
<Paragraph>
<Run Text="RichTextBox"/>
</Paragraph>
</FlowDocument>
</RichTextBox>
</UserControl>