How can I add this WPF control into my WinForm?

后端 未结 3 1852
一向
一向 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 15:10

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

提交回复
热议问题