0

how can I add a System.Windows.Forms.RichTextBox() to my window or grid. Without having to add a WindowsFormsHost(). Here is my code:

.xaml

             <Grid Name="Grid1">
             </Grid>
        </Window>

.xaml.cs

        Host1 = new WindowsFormsHost();
        Host1.Margin = new Thickness(8, 12, 3, 3);
        Host1.Height = 315;
        Host1.Width = 570;
        Host1.VerticalAlignment = System.Windows.VerticalAlignment.Top;
        Host1.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;

        richTextBox1 = new System.Windows.Forms.RichTextBox();
        richTextBox1.Height = 315;
        richTextBox1.Width = 570;
        richTextBox1.TextChanged += new EventHandler(richTextBox1_TextChanged);

        Host1.Child = richTextBox1;
        Grid1.Children.Add(Host1);

I want to remove my Host1 it makes the code too troublesome. How can I add directly to the window the richTextBox1 or directly to the Grid1 the richTextBox1?

2 Answers 2

2

The solution is quite simple: don't use the Windows Forms version of the control.

Instead look to use the RichTextBox included in System.Windows.Controls, or the free RichTextBox from the Community Edition of the WPF Toolkit.

Sign up to request clarification or add additional context in comments.

Comments

2

You can't.

Windows Forms and WPF are two completely different technologies with completely different infrastructures, rendering stacks, event models, input models, etc.

To host WinForms controls in WPF pages, you need something to host the WinForms control(s) and bridge the gaps between WPF and WinForms, marshalling events, etc. Conversely, to host WPF controls in WinForms pages, you have to use the ElementHost class.

You might want to consider using WPF's RichTextBox control instead.

Alternatively, if you find the WinForms RichText control too troublesome, consider using one of the many WPF RichText controls available from a number of vendors / sources.

3 Comments

Is there a control() version of windowsformhose() ?
Actually do you know how to implement it using ElementHost ?
Why do you want to use the WinForms RichText control rather than the WPF one?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.