简体   繁体   English

如何将此 WPF 控件添加到我的 WinForm 中?

[英]How can I add this WPF control into my WinForm?

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.我知道我必须使用ElementHost在 WinForm 中显示 WPF 控件,但由于 WPF 控件是第三方软件,它只附带一个 XML 文件和一个 DLL 文件。

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.控件是AvalonEdit ,我将ICSharpCode.AvalonEdit.xmlICSharpCode.AvalonEdit.dll文件都添加到我的项目中,然后我转到Project -> Add Reference并添加了 DLL 作为参考。 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.现在我可以在我的代码中访问ICSharpCode命名空间,所有的类和方法都公开了,但从这一点来看,我不确定如何在我的 WinForm 中实际使用该控件。

I was expecting a WPF control to appear in the Solution Explorer, but it does not.我期待 WPF 控件出现在解决方案资源管理器中,但它没有。 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.无论如何,我尝试向我的 WinForm 添加一个ElementHost控件,但是当我尝试选择托管内容时,没有出现任何控件,因此它不知道我的 WPF 控件。 How can I use the AvalonEdit WPF control in my WinForm?如何在我的 WinForm 中使用 AvalonEdit WPF 控件?

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.实现这一目标的一种方法是创建一个自定义 WPF 用户控件,其中包含您要使用的 AvalonEdit 组件。 Ie IE

  1. Create a WPF User Control library project and create a user control containing the AvalonEdit component.创建一个 WPF 用户控件库项目并创建一个包含 AvalonEdit 组件的用户控件。

  2. Add the User control project to your Winforms solution.将用户控件项目添加到 Winforms 解决方案。

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:或者您可以直接在代码中添加 AvalonEdit 控件,如下所示:

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.不确定控件的名称,因此请根据需要替换 AvalonEditControl。

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);
}

这是结果

        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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM