我正在尝试将项目列表绑定到TabControl.这些项目看起来像:
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);
}
}
....
在我的主窗口中,List被称为“allScripts”.这是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>
问题是我无法在WindowsFormsHost中设置“Child”,因为
A ‘Binding’ cannot be set on the ‘Child’ property of type ‘WindowsFormsHost’. A ‘Binding’ can only be set on a DependencyProperty of a DependencyObject.
如何设置WindowsFormsHost子项?
编辑:忘了提,在主窗口构造函数我有:
tabControl1.ItemsSource = allScripts;
解决方法:
将您的内容模板更改为
<TabControl.ContentTemplate>
<DataTemplate>
<ContentControl Content="{Binding Editor}" />
</DataTemplate>
</TabControl.ContentTemplate>
并将代码隐藏的Editor属性更改为
public WindowsFormsHost Editor
{
get { return new WindowsFormsHost(){Child=editor}; }
}