0

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>
Adrian Brown
  • 73
  • 1
  • 9
  • What do you mean by this: "This hosts a usercontrol containing a WPF RichTextBox". can you show part of xaml code that is relevant. – Aryan Firouzian Sep 20 '17 at 12:15
  • The elementHost hosts UserControl1 which contains a RichTextBox. I thought the only way to host a WPF control on a winform was via a UserControl? – Adrian Brown Sep 20 '17 at 12:17

1 Answers1

0

I solved the problem by creating the element host at runtime on the winform. I declared the following on the form;

public ElementHost host = new ElementHost();
    public System.Windows.Controls.RichTextBox wpfRichText = new System.Windows.Controls.RichTextBox();

Then added wpfRichText as the child for host.

later in the form I had a button which does the following;

    string richText = new TextRange(wpfRichText.Document.ContentStart, wpfRichText.Document.ContentEnd).Text;
        textBox1.Text = richText;

This pulls the text from the RichTextBox hosted in the element host and adds it to the textbox(as an example - I will actually use the text for other purposes).

Adrian Brown
  • 73
  • 1
  • 9