Binding TabControl ItemsSource to a Dictionary?...

Nov 20, 2012 at 5:38 AM

Hi,

How do I successfully bind a TabControl to a Dictionary?

Developing MVVM:

Object:

public class MainRegionTabs
{
 private string _header;
 private string _content;

 public MainRegionTabs(string header, string content)
 {

   _header = header;
   _content = content;

 }

 public string Header
 {
  get { return _header; }
 }

 public string Content
 {
  get { return _content; }
 }
}

Shell:

<Window.Resources>
 <DataTemplate x:Key="HeaderTemplate">
  <TextBlock Text="{Binding Path=Header}" />
 </DataTemplate>

 <DataTemplate x:Key="ContentTemplate">
  <TextBlock Text="{Binding Path=Content}" />
 </DataTemplate>
</Window.Resources>

<Grid x:Name="ContentGrid" Grid.Row="1">
 <TabControl
  Style="{StaticResource ContentGrid}"
  SelectedIndex="0"
  VerticalAlignment="Stretch"
  ItemContainerStyle="{StaticResource ShellTabItemStyle}"
  Background="{StaticResource headerBarBG}"
  ItemsSource="{Binding MainRegionTabItems, Mode=OneWay}"
  ItemTemplate="{StaticResource HeaderTemplate}"
  ContentTemplate="{StaticResource ContentTemplate}">
 </TabControl>
</Grid>
ShellViewModel:
 
public class ShellViewModel : ViewModelBase
{
  private static Dictionary<string, MainRegionTabs> _dataCollection = new Dictionary<string, MainRegionTabs>();
...
..

....Binding to the property.
public Dictionary<string, MainRegionTabs> MainRegionTabItems
{
  get { return _dataCollection; }
}

...
..

....Adding the tab from a subscribed event.
public void OnTurahSheetEvent(TurahSheetTab tab)
{
  if (!_dataCollection.ContainsKey(tab.SheetName))
  {
   MainRegionTabs ntab = new MainRegionTabs(
    tab.SheetName,
    tab.SheetId + ", " + tab.RelationshipId);

    _dataCollection.Add(tab.SheetName, ntab);
  }
}
How do I set the Binding properties?

 

Thanks!...