如何将 List 集合绑定到 WPF 中的 TabControl 标头?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/589802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:04:37  来源:igfitidea点击:

How can I bind a List collection to TabControl headers in WPF?

wpfdata-bindingtabcontrol

提问by Edward Tanguay

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

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

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

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

XAML:

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>  

code behind:

后面的代码:

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; }
}

采纳答案by Joachim Kerschbaumer

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

只需将您的列表绑定到您的 TabControl 作为 ItemsSource,例如

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

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

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

回答by Christof

Here ist what I would do

这是我会做的

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        //create all 
        var customers = new List<Customer>{
            new Customer {FirstName = "Jim", LastName = "Smith", NumberOfContracts = 23},
            new Customer {FirstName = "Jane", LastName = "Smith", NumberOfContracts = 23},
            new Customer {FirstName = "John", LastName = "Tester", NumberOfContracts = 23}};

        //show 
        TheTabControl.ItemsSource = customers;
        TheTabControl.SelectedIndex = 0;
    }


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

And on the XAML side

在 XAML 方面

<TabControl x:Name="TheTabControl">            
    <TabControl.ItemTemplate>
        <DataTemplate>                    
            <TextBlock>                            
                <TextBlock Text="{Binding FirstName}"/> <TextBlock Text="{Binding LastName}"/>
            </TextBlock>                        
        </DataTemplate>
    </TabControl.ItemTemplate>
    <TabControl.ContentTemplate>
        <DataTemplate>
            <TextBlock>                            
                This is <TextBlock Text="{Binding FirstName}"/> <TextBlock Text="{Binding LastName}"/>
            </TextBlock>
        </DataTemplate>
    </TabControl.ContentTemplate>
</TabControl>

回答by Joachim Kerschbaumer

Your answer can be found here.

您可以在此处找到答案。

http://www.codeplex.com/smartclient/Thread/View.aspx?ThreadId=31821

http://www.codeplex.com/smartclient/Thread/View.aspx?ThreadId=31821

Notice how he sets the ContentTemplate as well as the ItemTemplate...you almost had it!

注意他是如何设置 ContentTemplate 和 ItemTemplate 的……你差点就搞定了!