How can I add this WPF control into my WinForm?

后端 未结 3 1841
一向
一向 2020-12-24 14:15

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 a

3条回答
  •  囚心锁ツ
    2020-12-24 14:53

    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.

提交回复
热议问题