与WindowsFormsHost绑定 [英] Bind with WindowsFormsHost
问题描述
我正在尝试将项目列表绑定到TabControl.这些项目看起来像:
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);
}
}
....
在我的主窗口中,列表称为"allScripts".这是XAML:
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>
问题是我无法在WindowsFormsHost中设置子级",因为
The problem is I can't set "Child" in WindowsFormsHost because
不能在"WindowsFormsHost"类型的"Child"属性上设置"Binding".只能在DependencyObject的DependencyProperty上设置绑定".
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子级?
How can I set the WindowsFormsHost child?
忘了提及,在主窗口构造函数中,我有:
forgot to mention, in the main window constructor I have:
tabControl1.ItemsSource = allScripts;
推荐答案
将内容模板更改为
<TabControl.ContentTemplate>
<DataTemplate>
<ContentControl Content="{Binding Editor}" />
</DataTemplate>
</TabControl.ContentTemplate>
并将代码隐藏的Editor
属性更改为
and change the Editor
property of your code-behind to
public WindowsFormsHost Editor
{
get { return new WindowsFormsHost(){Child=editor}; }
}
这篇关于与WindowsFormsHost绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!