本文介绍了WPF TabControl 数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个包含 TabControl 和 TextBlock 的 WPF 用户界面.

I'm trying to build a WPF user interface containing a TabControl, and a TextBlock.

我想将这两个控件绑定到以下类的基础实例集合:

I want to bind these two controls to an underlying collection of instances of the following class:

class PageModel
{
  public string Title {get;set;}
  public string TabCaption {get;set;}
  public FrameworkElement TabContent {get;set}
}

选项卡控件应为每个 PageModel 显示一个选项卡.

The tab control should display a tab for each PageModel.

  • 每个选项卡的标题应显示 TabCaption 属性
  • 每个选项卡的内容都应该是 TabContent 属性.

TextBlock 应显示当前选定选项卡的标题.

The TextBlock should display the Title of the currently selected tab.

我怎样才能达到这个结果?

How can I achieve this result?

推荐答案

<TabControl x:Name="_tabControl" ItemsSource="{Binding PageModels}">
    <TabControl.ItemContainerStyle>
        <Style TargetType="TabItem">
            <Setter Property="Header" Value="{Binding TabCaption}"/>
            <Setter Property="Content" Value="{Binding TabContent}"/>
        </Style>
    </TabControl.ItemContainerStyle>
</TabControl>
<TextBlock Text="{Binding SelectedItem.Title, ElementName=_tabControl}"/>

这篇关于WPF TabControl 数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 08:16