views:

16

answers:

1

I'm trying to write a text editor in WPF and I have a problem trying to locate the correct instance of an editor within a TabControl in response to a File -> Open action.

Tab items are added programatically and contain a WindowsFormsHost instance which in turn allows each tab to display an editor provided by the ScintillaNet WinForms component.

When a tab is selected and a user selects File -> Open, I need to locate the correct WindowsFormsHost instance based on the tab selection so I can load the file into the correct Scintilla instance.

Previously, I'd done this in WinForms simply by doing:

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

How does this work in WPF?

A: 

To follow up regarding the solution I've gone with for now: I've decided to subclass the TabItem class and hold an additional property that references the WinForms ScintillaNet control:

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

And when I add new tabs, I just make sure that EditorControl is set to the new instance of Scintilla that is created too:

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

Now when an event is raised, I can query the selected tab and as cast to CustomTabItem in order to access the reference to the respective editor:

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

Hope that helps someone else.

Mark