WPF Tab control, binding tabs and content


/ Published in: C#
Save to your folder(s)

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


Copy this code and paste it in your HTML
  1. ----------------------------- [XAML] -----------------------------
  2. <Window x:Class="TabBinding.MainWindow"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
  6. Title="MainWindow" Height="350" Width="525">
  7.  
  8. <Grid Name="MainGrid">
  9. <TabControl Name="MyTabControl" ItemsSource="{Binding Countries}" >
  10.  
  11. <TabControl.ItemTemplate>
  12. <DataTemplate>
  13. <TextBlock Text="{Binding CountryName}" />
  14. </DataTemplate>
  15. </TabControl.ItemTemplate>
  16.  
  17. <TabControl.ContentTemplate>
  18. <DataTemplate>
  19. <ListView Name="ScenarioListBox" ItemsSource="{Binding People}" MinHeight="20" SelectionMode="Single" >
  20. <ListView.View>
  21. <GridView>
  22. <GridViewColumn Header="Name" Width="200">
  23. <GridViewColumn.CellTemplate>
  24. <DataTemplate>
  25. <Label Content="{Binding Path=Name}" />
  26. </DataTemplate>
  27. </GridViewColumn.CellTemplate>
  28. </GridViewColumn>
  29. </GridView>
  30. </ListView.View>
  31. </ListView>
  32. </DataTemplate>
  33. </TabControl.ContentTemplate>
  34.  
  35. </TabControl>
  36. </Grid>
  37. </Window>
  38.  
  39.  
  40. ----------------------------- [.CS] ----------------------------------
  41. using System;
  42. using System.Collections.Generic;
  43. using System.Collections.ObjectModel;
  44. using System.Linq;
  45. using System.Text;
  46. using System.Threading.Tasks;
  47. using System.Windows;
  48. using System.Windows.Controls;
  49. using System.Windows.Data;
  50. using System.Windows.Documents;
  51. using System.Windows.Input;
  52. using System.Windows.Media;
  53. using System.Windows.Media.Imaging;
  54. using System.Windows.Navigation;
  55. using System.Windows.Shapes;
  56.  
  57. namespace TabBinding
  58. {
  59. public class Person
  60. {
  61. public String Name { get; set; }
  62. }
  63.  
  64. public class Country
  65. {
  66. public String CountryName { get; set; }
  67. public ObservableCollection<Person> People { get; set; }
  68.  
  69. public Country()
  70. {
  71. People = new ObservableCollection<Person>();
  72. }
  73. }
  74.  
  75.  
  76. /// <summary>
  77. /// Interaction logic for MainWindow.xaml
  78. /// </summary>
  79. ///
  80. public partial class MainWindow : Window
  81. {
  82. public ObservableCollection<Country> Countries { get; set; }
  83.  
  84. public MainWindow()
  85. {
  86. InitializeComponent();
  87. MainGrid.DataContext = this;
  88.  
  89. Countries = new ObservableCollection<Country>();
  90.  
  91. Country England = new Country() { CountryName = "England" };
  92. England.People.Add(new Person() { Name = "Ian" });
  93.  
  94. Country SAfrica = new Country() { CountryName = "s. Africa" };
  95. SAfrica.People.Add(new Person() { Name = "John" });
  96.  
  97. Country Zimbabwe = new Country() { CountryName = "Zimbabwe" };
  98. Zimbabwe.People.Add(new Person() { Name = "XXX" });
  99. Zimbabwe.People.Add(new Person() { Name = "YYY" });
  100.  
  101. Countries.Add(England);
  102. Countries.Add(SAfrica);
  103. Countries.Add(Zimbabwe);
  104. }
  105. }
  106. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.