views:

110

answers:

1

In silverlight app I create tab items dynamically in code (MainView's code-behind):

TabItem tab = new TabItem();
CustomerView view = new CustomerView();
view.DataContext = customerViewModel; //or tab.DataContext = customerViewModel;??
tab.Content = view;
DataTemplate template = this.Resources["CustomTabItemHeader"] as DataTemplate;
tab.HeaderTemplate = template;
tabControl.Items.Add(tab);

CustomTabItemHeader (in MainView.xaml) looks like:

<UserControl.Resources>
    <DataTemplate x:Key="CustomTabItemHeader">
        <TextBlock Text="{Binding Path=DisplayName}"/>
    </DataTemplate>
</UserControl.Resources>

CustomerViewModel has DisplayName property and it implements INotifyPropertyChanged interface. But DisplayName isn't displayed in the tab header. Can someone explain me why?

A: 

I foolishly assumed that textblock in the TabItem header template would look into tab's datacontext in order to get its text value, which was not the case. So I had to do the following:

tab.Header = customerViewModel; 
synergetic