我如何添加这个WPF控件到我的WinForm? [英] How can I add this WPF control into my WinForm?

查看:1325
本文介绍了我如何添加这个WPF控件到我的WinForm?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,我必须使用 ElementHost的来显示一个WinForm一个WPF控件,但由于WPF控件是第三方软件,而且只配备了一个XML文件和一个DLL文件。



控制是的 AvalonEdit ,我加了两个 ICSharpCode.AvalonEdit.xml ICSharpCode.AvalonEdit.dll 文件到我的项目,我去了项目 - >添加引用,并添加DLL作为参考。现在,我可以访问我的代码 ICSharpCode 命名空间,所有的类和方法的暴露,但从这点我不能确定如何实际使用在我的WinForm控件。



我期待一个WPF控件出现在Solution Explorer中,但事实并非如此。我尝试添加一个 ElementHost的控制我的WinForm反正,但是当我尝试选择托管内容,不控制会出现,因此它不知道我的WPF控件。我如何使用我的WinForm的AvalonEdit WPF控件?


解决方案

如果您希望能够设置在设计中承载的内容时间控制需要您的解决方案的一部分。实现方法之一就是创建一个包含您要使用的AvalonEdit组件的自定义WPF用户控件。即




  1. 创建WPF用户控件库项目,并创建包含AvalonEdit组件的用户控件


  2. 在用户控件项目添加到您的WinForms解决方案。




< 。p>现在,你应该能够选择新的用户控件作为承载的内容



或者你可以直接在这样的代码添加AvalonEdit控制:

 公共Form1中()
{
的InitializeComponent();

ElementHost的主机=新ElementHost的();
host.Size =新的大小(200,100);
host.Location =新点(100,100);

Av​​alonEditControl编辑=新AvalonEditControl();
host.Child =编辑;

this.Controls.Add(主机);
}



不知道该控件调用,所以更换AvalonEditControl为宜。


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?

解决方案

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.

这篇关于我如何添加这个WPF控件到我的WinForm?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆