gpt4 book ai didi

c# - 与 WindowsFormsHost 绑定(bind)

转载 作者:太空狗 更新时间:2023-10-30 00:43:31 27 4
gpt4 key购买 nike

我正在尝试将项目列表绑定(bind)到 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);
}
}
....

在我的主窗口中,列表名为“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}; }
}

关于c# - 与 WindowsFormsHost 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10885211/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com