如何将 List 集合绑定到 WPF 中的 TabControl 标头? [英] How can I bind a List collection to TabControl headers in WPF?

查看:35
本文介绍了如何将 List 集合绑定到 WPF 中的 TabControl 标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以将数据输入我的 TabControl,但标题周围有框架,我无法从一个选项卡到另一个选项卡.

I can get data into my TabControl but the headers have frames around them and I can't slick from tab to tab.

这个 TabControl 上的 XAML 绑定语法有什么问题?

What am I doing wrong with the XAML binding syntax on this TabControl?

XAML:

<StackPanel>
    <TabControl x:Name="TheTabControl">
        <TabControl.ItemTemplate>
            <DataTemplate>
                <TabItem Header="{Binding LastName}">
                    <StackPanel Margin="10" Orientation="Horizontal">
                        <TextBlock Text="{Binding FirstName}"/>
                        <TextBlock Text=" "/>
                        <TextBlock Text="{Binding LastName}"/>
                    </StackPanel>
                </TabItem>
            </DataTemplate>                
        </TabControl.ItemTemplate>
    </TabControl>

    <TabControl>
        <TabItem Header="Tab1">
            <TextBlock Text="This is a test of tab 1"/>
        </TabItem>
        <TabItem Header="Tab2">
            <TextBlock Text="This is a test of tab 2"/>
        </TabItem>
    </TabControl>

</StackPanel>  

背后的代码:

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();

        //create all
        List<Customer> customers = new List<Customer>();
        customers.Add(new Customer { FirstName = "Jim", LastName = "Smith", NumberOfContracts = 23 });
        customers.Add(new Customer { FirstName = "Jane", LastName = "Smith", NumberOfContracts = 23 });
        customers.Add(new Customer { FirstName = "John", LastName = "Tester", NumberOfContracts = 23 });

        //show
        TheListBox.ItemsSource = customers;

    }
}

public class Customer
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int NumberOfContracts { get; set; }
}

推荐答案

只需将 List 作为 ItemsSource 绑定到 TabControl,例如

just bind your List to your TabControl as ItemsSource, e.g.

<TabControl ItemsSource="{Binding Customers}"/>

这将为您提供客户中每个对象的选项卡.

this will give you a tab for each object in customer.

这篇关于如何将 List 集合绑定到 WPF 中的 TabControl 标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆