Guest User

Untitled

a guest
Jan 15th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. <TabControl>
  2. <TabItem>
  3. <local:MyUserControl/>
  4. </TabItem>
  5. </TabControl>
  6.  
  7. <TabControl ItemsSource="{Binding TabCollection}">
  8. <TabItem Header="{Binding Header}"
  9. Source="{Binding MyUserControl}"> <!-- Source is obviously not a property of TabItem-->
  10. <!-- I'm just using it in this context as an example. Like how a 'Frame' would work -->
  11. </TabItem>
  12. </TabControl>
  13.  
  14. public class TabViewModel
  15. {
  16. // 'TabModel' is a simple class that acts as a Model for this class (TabViewModel)
  17. // All it does is store strings, integers, etc. as properties
  18. // i.e: the fields 'Header' and 'MyUserControl' in the below method are both strings declared in 'TabModel'
  19. public ObservableCollection<TabModel> TabCollection { get; set; }
  20.  
  21. public TabViewModel()
  22. {
  23. TabCollection = new ObservableCollection<TabModel>();
  24. PopulateTabCollection();
  25. }
  26.  
  27. private void PopulateTabCollection()
  28. {
  29. TabCollection.Add(new TabModel()
  30. {
  31. Header = "FirstUserControl",
  32. MyUserControl = "Views/MyFirstUserControl.xaml"
  33. });
  34.  
  35. TabCollection.Add(new TabModel()
  36. {
  37. Header = "SecondUserControl",
  38. MyUserControl = "Views/MySecondUserControl.xaml"
  39. });
  40. }
  41. }
  42.  
  43. <Window.Resources>
  44. <DataTemplate DataType="{x:Type local:PersonVm}">
  45. <local:Person/>
  46. </DataTemplate>
  47. <DataTemplate DataType="{x:Type local:DeptVm}">
  48. <local:Department/>
  49. </DataTemplate>
  50. </Window.Resources>
  51. <Grid>
  52. <TabControl ItemsSource="{Binding TabCollection}" SelectedIndex="0">
  53. <TabControl.ItemTemplate>
  54. <DataTemplate>
  55. <TextBlock Text="{Binding TabName}"/>
  56. </DataTemplate>
  57. </TabControl.ItemTemplate>
  58. <TabControl.ContentTemplate>
  59. <DataTemplate>
  60. <ContentControl Content="{Binding }"/>
  61. </DataTemplate>
  62. </TabControl.ContentTemplate>
  63. </TabControl>
  64. </Grid>
  65.  
  66.  
  67. public partial class MainWindow : Window
  68. {
  69. public MainWindow()
  70. {
  71. InitializeComponent();
  72. this.DataContext = new TabViewModel();
  73. }
  74. }
  75.  
  76. public class TabViewModel
  77. {
  78. public ObservableCollection<object> TabCollection { get; set; }
  79.  
  80. public TabViewModel()
  81. {
  82. TabCollection = new ObservableCollection<object>();
  83. PopulateTabCollection();
  84. }
  85.  
  86. private void PopulateTabCollection()
  87. {
  88. TabCollection.Add(new PersonVm()
  89. {
  90. PersonName = "FirstUserControl",
  91. Address = "Address",
  92. TabName = "Person Tab"
  93. });
  94.  
  95. TabCollection.Add(new DeptVm()
  96. {
  97. DeptName = "SecondUserControl",
  98. TabName = "DeptTab"
  99. });
  100. }
  101. }
  102.  
  103. public class PersonVm
  104. {
  105. public string PersonName { get; set; }
  106. public string Address { get; set; }
  107. public string TabName { get; set; }
  108.  
  109. }
  110.  
  111. public class DeptVm
  112. {
  113. public string DeptName { get; set; }
  114. public string TabName { get; set; }
  115. }
Add Comment
Please, Sign In to add comment