如何将列表集合绑定到WPF中的表控制头?

时间:2022-05-01 14:14:33

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?

我在这个表格控件上的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; }
}

4 个解决方案

#1


6  

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

将你的列表绑定到你的列表控件作为ItemsSource,例如。

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

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

这将为customer中的每个对象提供一个选项卡。

#2


46  

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>

#3


5  

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…你几乎把它!

#4


3  

I found a solution here:

我在这里找到了一个解决方案:

http://social.msdn.microsoft.com/forums/en-US/wpf/thread/956eaba3-53bd-4683-b3dd-28b20e4b7526/

http://social.msdn.microsoft.com/forums/en - us/wpf/thread/956eaba3 - 53 - bd - 4683 b3dd b20e4b7526/——28

It worked for me.

它为我工作。

#1


6  

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

将你的列表绑定到你的列表控件作为ItemsSource,例如。

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

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

这将为customer中的每个对象提供一个选项卡。

#2


46  

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>

#3


5  

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…你几乎把它!

#4


3  

I found a solution here:

我在这里找到了一个解决方案:

http://social.msdn.microsoft.com/forums/en-US/wpf/thread/956eaba3-53bd-4683-b3dd-28b20e4b7526/

http://social.msdn.microsoft.com/forums/en - us/wpf/thread/956eaba3 - 53 - bd - 4683 b3dd b20e4b7526/——28

It worked for me.

它为我工作。