【问题标题】:Bind with WindowsFormsHost与 WindowsFormsHost 绑定
【发布时间】:2012-06-04 17:07:05
【问题描述】:

我正在尝试将项目列表绑定到 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”,因为

不能在“WindowsFormsHost”类型的“子”属性上设置“绑定”。 “绑定”只能在 DependencyObject 的 DependencyProperty 上设置。

如何设置 WindowsFormsHost 子项?

编辑:忘了提,在主窗口构造函数中我有:

tabControl1.ItemsSource = allScripts;

【问题讨论】:

    标签: c# wpf windowsformshost


    【解决方案1】:

    将您的内容模板更改为

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

    并将代码隐藏的 Editor 属性更改为

    public WindowsFormsHost Editor   
    {   
        get { return new WindowsFormsHost(){Child=editor}; }   
    }   
    

    【讨论】:

      猜你喜欢
      • 2016-11-26
      • 2021-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-21
      • 1970-01-01
      相关资源
      最近更新 更多