Return to Snippet

Revision: 66732
at June 26, 2014 00:43 by imbraz


Initial Code
----------------------------- [XAML] -----------------------------
<Window x:Class="TabBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase" 
        Title="MainWindow" Height="350" Width="525">

    <Grid Name="MainGrid">
        <TabControl Name="MyTabControl" ItemsSource="{Binding Countries}" >

            <TabControl.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding CountryName}" />
                </DataTemplate>
            </TabControl.ItemTemplate>

            <TabControl.ContentTemplate>
                <DataTemplate>
                    <ListView Name="ScenarioListBox" ItemsSource="{Binding People}" MinHeight="20" SelectionMode="Single"  >
                        <ListView.View>
                            <GridView>
                                <GridViewColumn Header="Name" Width="200">
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <Label Content="{Binding Path=Name}" />
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>
                            </GridView>
                        </ListView.View>
                    </ListView>
                </DataTemplate>
            </TabControl.ContentTemplate>
            
        </TabControl>
    </Grid>
</Window>


----------------------------- [.CS] ----------------------------------
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TabBinding
{
    public class Person
    {
        public String Name { get; set; }
    }

    public class Country
    {
        public String  CountryName { get; set; }
        public ObservableCollection<Person> People { get; set; }

        public Country()
        {
            People = new ObservableCollection<Person>();
        }
    }


    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    /// 
    public partial class MainWindow : Window
    {
        public ObservableCollection<Country> Countries { get; set; }

        public MainWindow()
        {
            InitializeComponent();
            MainGrid.DataContext = this;

            Countries = new ObservableCollection<Country>();

            Country England = new Country() { CountryName = "England" };
            England.People.Add(new Person() { Name = "Ian" });

            Country SAfrica = new Country() { CountryName = "s. Africa" };
            SAfrica.People.Add(new Person() { Name = "John" });

            Country Zimbabwe = new Country() { CountryName = "Zimbabwe" };
            Zimbabwe.People.Add(new Person() { Name = "XXX" });
            Zimbabwe.People.Add(new Person() { Name = "YYY" });

            Countries.Add(England);
            Countries.Add(SAfrica);
            Countries.Add(Zimbabwe);
        }
    }
}

Initial URL

                                

Initial Description
This is a tab control whos tabs are bound and the content of each tab is also bound

Initial Title
WPF Tab control, binding tabs and content

Initial Tags

                                

Initial Language
C#