WPF选项卡Control/File open需要找到子WindowsFormsHost
我试图在WPF中编写一个文本编辑器,但在响应文件->打开操作时,试图在WPF选项卡Control/File open需要找到子WindowsFormsHost,wpf,windowsformshost,Wpf,Windowsformshost,我试图在WPF中编写一个文本编辑器,但在响应文件->打开操作时,试图在TabControl中找到编辑器的正确实例时遇到问题 选项卡项以编程方式添加,并包含一个WindowsFormsHost实例,该实例反过来允许每个选项卡显示闪烁网WinForms组件提供的编辑器 当选择选项卡并且用户选择File->Open时,我需要根据选项卡选择找到正确的WindowsFormsHost实例,以便将文件加载到正确的闪烁实例中 以前,我在WinForms中只是通过执行以下操作: tabControl.TabPa
TabControl
中找到编辑器的正确实例时遇到问题
选项卡项以编程方式添加,并包含一个WindowsFormsHost
实例,该实例反过来允许每个选项卡显示闪烁网WinForms组件提供的编辑器
当选择选项卡并且用户选择File->Open时,我需要根据选项卡选择找到正确的WindowsFormsHost实例,以便将文件加载到正确的闪烁实例中
以前,我在WinForms中只是通过执行以下操作:
tabControl.TabPages[tabControl.SelectedIndex].Controls.Find("Scintilla")
这在WPF中是如何工作的?要跟进我目前使用的解决方案:我决定将
选项卡项
类划分为子类,并保留一个引用WinForms闪烁网控件的附加属性:
public class CustomTabItem : TabItem
{
public Scintilla EditorControl
{
get; set;
}
}
当我添加新选项卡时,我只需确保EditorControl
被设置为创建的scatinlla
的新实例:
var editor = ScintillaFactory.Create();
var tab = new CustomTabItem()
{
Header = "Untitled",
Content = new WindowsFormsHost() { Name = "WinformsHost", Child = editor },
EditorControl = editor
};
tabControl.Items.Add(tab);
tab.Focus();
现在,当引发事件时,我可以查询所选选项卡并将其转换为CustomTabItem
,以便访问对相应编辑器的引用:
var editor = (tabControl.Items[tabControl.SelectedIndex] as CustomTabItem).EditorControl
editor.Text = "text here";
希望这能帮助别人