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

后端 未结 4 531
说谎
说谎 2020-12-09 09:02

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

What am I doing wrong with the XAML binding syntax on

相关标签:
4条回答
  • 2020-12-09 09:19

    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.

    0 讨论(0)
  • 2020-12-09 09:31

    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

    <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>
    
    0 讨论(0)
  • 2020-12-09 09:39

    Your answer can be found here.

    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!

    0 讨论(0)
  • 2020-12-09 09:45

    I found a solution here:

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

    It worked for me.

    0 讨论(0)
提交回复
热议问题