Question

I am trying to bind a List of items to a TabControl. The items look like:

class SciEditor
{
    private Scintilla editor = null;
    public System.Windows.Forms.Control Editor
    {
        get { return editor; }
    }

    private string path = null;
    public string ShortName
    {
        get
        {
            return null == path ? "New Script" : Path.GetFileNameWithoutExtension(path);
        }
    }
    ....

In my main window, the List is called "allScripts". Here's the XAML:

<TabControl Grid.Row="0" Grid.Column="0" Name="tabControl1">
            <TabControl.ItemTemplate>
                <DataTemplate>
                    <TextBlock>                            
                        <TextBlock Text="{Binding ShortName}"/>
                    </TextBlock>
                </DataTemplate>
            </TabControl.ItemTemplate>
            <TabControl.ContentTemplate>
                <DataTemplate>
                    <WindowsFormsHost Child="{Binding Editor}" />
                </DataTemplate>
            </TabControl.ContentTemplate>
</TabControl>

The problem is I can't set "Child" in WindowsFormsHost because

A 'Binding' cannot be set on the 'Child' property of type 'WindowsFormsHost'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.

How can I set the WindowsFormsHost child?

EDIT: forgot to mention, in the main window constructor I have:

tabControl1.ItemsSource = allScripts;
Was it helpful?

Solution

Change your content template to

<TabControl.ContentTemplate> 
     <DataTemplate> 
          <ContentControl Content="{Binding Editor}" /> 
     </DataTemplate> 
</TabControl.ContentTemplate> 

and change the Editor property of your code-behind to

public WindowsFormsHost Editor   
{   
    get { return new WindowsFormsHost(){Child=editor}; }   
}   
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top