Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 24th, 2012  |  syntax: None  |  size: 1.30 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to create tab-able content in WPF/C#?
  2. <Page.DataContext>
  3.     <Samples:TabBindingViewModels />
  4. </Page.DataContext>
  5.  
  6. <Grid>
  7.     <Grid.Resources>
  8.         <DataTemplate x:Key="ContentTemplate"
  9.                       DataType="{x:Type Samples:TabBindingViewModel}">
  10.             <TextBlock Text="{Binding Content}"/>
  11.         </DataTemplate>
  12.     </Grid.Resources>
  13.     <TabControl ContentTemplate="{StaticResource ContentTemplate}"  
  14.                 DisplayMemberPath="Header" ItemsSource="{Binding Items}" />
  15. </Grid>
  16.        
  17. public class TabBindingViewModels
  18. {
  19.     public TabBindingViewModels()
  20.     {
  21.         Items = new ObservableCollection<TabBindingViewModel>
  22.                     {
  23.                         new TabBindingViewModel(1),
  24.                         new TabBindingViewModel(2),
  25.                         new TabBindingViewModel(3),
  26.                     };
  27.     }
  28.  
  29.     public IEnumerable<TabBindingViewModel> Items { get; private set; }
  30. }
  31.  
  32. public class TabBindingViewModel
  33. {
  34.     public TabBindingViewModel() : this(0)
  35.     {
  36.     }
  37.  
  38.     public TabBindingViewModel(int n)
  39.     {
  40.         Header = "I'm the header: " + n.ToString(CultureInfo.InvariantCulture);
  41.         Content = "I'm the content: " + n.ToString(CultureInfo.InvariantCulture);
  42.     }
  43.  
  44.     public string Header { get; set; }
  45.     public string Content { get; set; }
  46. }