17

I know that I must use an ElementHost to display a WPF control in a WinForm, but as the WPF control is third party software and it only comes with an XML file and a DLL file.

The control is AvalonEdit, I added both the ICSharpCode.AvalonEdit.xml and ICSharpCode.AvalonEdit.dll files to my project, and I went to Project -> Add Reference and added the DLL as a reference. Now I can access the ICSharpCode namespace in my code, all of the classes and methods are exposed, but from this point I am unsure how to actually use the control in my WinForm.

I was expecting a WPF control to appear in the Solution Explorer, but it does not. I tried adding an ElementHost control to my WinForm anyways, but when I try to Select the Hosted Content, no controls appear, so it doesn't know about my WPF control. How can I use the AvalonEdit WPF control in my WinForm?

Brandon Miller
  • 2,157
  • 7
  • 31
  • 52
  • 3
    To the person who down-voted my question, it would be nice if you could leave a comment telling me why my question is bad. – Brandon Miller Jan 05 '13 at 08:58

3 Answers3

20

If you want to be able to set the hosted content at design time the control needs to be part of your solution. One way to achieve that is to create a custom WPF user control which contains the AvalonEdit component you want to use. I.e

  1. Create a WPF User Control library project and create a user control containing the AvalonEdit component.

  2. Add the User control project to your Winforms solution.

Now you should be able to select your new user control as the hosted content.

Or you could add the AvalonEdit control directly in code like this:

public Form1()
{
  InitializeComponent();

  ElementHost host= new ElementHost();
  host.Size = new Size(200, 100);
  host.Location = new Point(100,100);

  AvalonEditControl edit = new AvalonEditControl();
  host.Child = edit;

  this.Controls.Add(host);
}

Not sure what the control is called so replace the AvalonEditControl as appropriate.

Tommy Grovnes
  • 4,118
  • 2
  • 23
  • 38
  • 2
    The name of the control is `AvalonEdit.TextEditor`, and I tried that, it says it cannot be casted to a `Control`. I tried this: `TextEditor editor = new TextEditor(); elementHost1.Child = editor; this.Controls.Add((Control)editor);` Why can this not be casted to a control? – Brandon Miller Jan 05 '13 at 09:09
  • 4
    Change `this.Controls.Add((Control)editor)` to `this.Controls.Add(elementHost1)` – Tommy Grovnes Jan 05 '13 at 09:16
  • Ohhhh, I overlooked that you added the host, not the control itself. That did the trick!! Thank you so much! – Brandon Miller Jan 05 '13 at 10:05
  • Very sorry, I meant to do that. Your answer was a lifesaver. – Brandon Miller Jan 05 '13 at 10:57
9
public Form1()
{
    InitializeComponent();
    ICSharpCode.AvalonEdit.TextEditor textEditor = new ICSharpCode.AvalonEdit.TextEditor();
    textEditor.ShowLineNumbers = true;
    textEditor.FontFamily = new System.Windows.Media.FontFamily("Consolas");
    textEditor.FontSize = 12.75f;

    string dir = @"C:\Temp\";
    #if DEBUG
    dir = @"C:\Dev\Sandbox\SharpDevelop-master\src\Libraries\AvalonEdit\ICSharpCode.AvalonEdit\Highlighting\Resources\";
    #endif

    if (File.Exists(dir + "CSharp-Mode.xshd"))
    {
      Stream xshd_stream = File.OpenRead(dir + "CSharp-Mode.xshd");
      XmlTextReader xshd_reader = new XmlTextReader(xshd_stream);    
      // Apply the new syntax highlighting definition.
      textEditor.SyntaxHighlighting = ICSharpCode.AvalonEdit.Highlighting.Xshd.HighlightingLoader.Load(xshd_reader, ICSharpCode.AvalonEdit.Highlighting.HighlightingManager.Instance);
      xshd_reader.Close();
      xshd_stream.Close();
    }
    //Host the WPF AvalonEdiot control in a Winform ElementHost control
    ElementHost host = new ElementHost();
    host.Dock = DockStyle.Fill;
    host.Child = textEditor;
    this.Controls.Add(host);
}
Jeremy Thompson
  • 52,213
  • 20
  • 153
  • 256
0

this is result

        ElementHost host = new ElementHost();
        host.Size = new Size(200, 100);
        host.Location = new Point(100, 100);

        ICSharpCode.AvalonEdit.TextEditor edit = new 
        ICSharpCode.AvalonEdit.TextEditor();

        host.Child = edit;

        this.Controls.Add(host);