0

我正在尝试在 WPF 中编写一个文本编辑器,但在尝试在 a 中找到正确的编辑器实例TabControl以响应 File -> Open 操作时遇到问题。

选项卡项以编程方式添加并包含一个WindowsFormsHost实例,该实例反过来允许每个选项卡显示由 ScintillaNet WinForms 组件提供的编辑器。

当一个选项卡被选中并且用户选择 File -> Open 时,我需要根据选项卡选择找到正确的 WindowsFormsHost 实例,以便我可以将文件加载到正确的 Scintilla 实例中。

以前,我只是通过以下方式在 WinForms 中完成此操作:

tabControl.TabPages[tabControl.SelectedIndex].Controls.Find("Scintilla")

这在 WPF 中是如何工作的?

4

1 回答 1

0

跟进我现在使用的解决方案:我决定子类化TabItem该类并持有一个引用 WinForms ScintillaNet 控件的附加属性:

public class CustomTabItem : TabItem
{
    public Scintilla EditorControl
    {
        get; set;
    }
}

当我添加新选项卡时,我只需确保将EditorControl其设置为也创建的新实例Scintilla

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

现在,当引发事件时,我可以查询选定的选项卡并as转换CustomTabItem为以访问对相应编辑器的引用:

var editor = (tabControl.Items[tabControl.SelectedIndex] as CustomTabItem).EditorControl
editor.Text = "text here";

希望对其他人有所帮助。

于 2010-09-07T15:32:13.993 回答