Bind with WindowsFormsHost
Asked Answered
D

1

4

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;
Derrick answered 4/6, 2012 at 17:7 Comment(0)
V
9

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}; }   
}   
Vanguard answered 4/6, 2012 at 17:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.