Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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; }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
815 views
Welcome To Ask or Share your Answers For Others

1 Answer

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...