C# 如何将此WPF控件添加到WinForm中?
我知道我必须使用C# 如何将此WPF控件添加到WinForm中?,c#,wpf,winforms,avalonedit,C#,Wpf,Winforms,Avalonedit,我知道我必须使用ElementHost在WinForm中显示WPF控件,但由于WPF控件是第三方软件,它只附带XML文件和DLL文件 控件是,我将ICSharpCode.AvalonEdit.xml和ICSharpCode.AvalonEdit.dll文件添加到我的项目中,然后转到project->Add Reference并将dll添加为引用。现在我可以访问代码中的ICSharpCode名称空间,所有的类和方法都已公开,但从这一点上看,我不确定如何在WinForm中实际使用该控件 我希望WPF
ElementHost
在WinForm中显示WPF控件,但由于WPF控件是第三方软件,它只附带XML文件和DLL文件
控件是,我将ICSharpCode.AvalonEdit.xml
和ICSharpCode.AvalonEdit.dll
文件添加到我的项目中,然后转到project->Add Reference
并将dll添加为引用。现在我可以访问代码中的ICSharpCode
名称空间,所有的类和方法都已公开,但从这一点上看,我不确定如何在WinForm中实际使用该控件
我希望WPF控件出现在解决方案资源管理器中,但它没有。我尝试将
ElementHost
控件添加到我的WinForm中,但当我尝试选择托管内容时,没有控件出现,因此它不知道我的WPF控件。如何在WinForm中使用AvalonEdit WPF控件?如果希望能够在设计时设置托管内容,则该控件需要成为解决方案的一部分。实现这一点的一种方法是创建一个自定义WPF用户控件,其中包含要使用的AvaloneEdit组件。即
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);
}
不确定该控件的名称,因此请根据需要更换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);
}
对于投票否决我的问题的人,如果你能留下一条评论告诉我为什么我的问题不好,那就太好了。该控件的名称是
AvalonEdit.TextEditor
,我试过了,它说它不能转换为控件。我试过:TextEditor=newtextEditor();elementHost1.Child=编辑器;this.Controls.Add((控件)编辑器)
为什么不能将其强制转换为控件?将this.Controls.Add((控件)编辑器)
更改为this.Controls.Add(elementHost1)
ohhh,我忽略了您添加的是主机,而不是控件本身。这就成功了!!非常感谢你!非常抱歉,我是故意的。你的回答是救命恩人。
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);