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

查看:184
本文介绍了如何将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; }
}


推荐答案

您的TabControl作为ItemsSource,例如

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天全站免登陆