【问题标题】:WPF TabControl / File open needs to locate child WindowsFormsHostWPF TabControl/文件打开需要定位子WindowsFormsHost
【发布时间】:2010-09-07 13:43:00
【问题描述】:

我正在尝试在 WPF 中编写文本编辑器,但在尝试在 TabControl 中定位正确的编辑器实例以响应文件 -> 打开操作时遇到问题。

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

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

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

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

这在 WPF 中是如何工作的?

【问题讨论】:

    标签: wpf windowsformshost


    【解决方案1】:

    为了跟进我现在使用的解决方案:我决定继承 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";
    

    希望对其他人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-12
      • 2018-04-21
      • 1970-01-01
      • 1970-01-01
      • 2014-07-04
      • 1970-01-01
      相关资源
      最近更新 更多