Silverlight 4 TabControl MVVM tab header binding
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 = cust开发者_如何学GoomerViewModel;??
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?
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;
精彩评论