Hi
I have a xamTabControl whose ItemsSource is bound to an observable collection of viewmodels and need to have editable tab headers. I am using the example found here:
http://blogs.infragistics.com/blogs/alex_fidanov/archive/2009/10/01/how-to-rename-the-tabitems-of-the-xamtabcontrol-runtime-like-excel.aspx
But the problem is that instead of the Header property, the object type ToString() is being displayed in the tab header. Any idea what I might have done wrong and how I can get the actual header property displayed?
In my view I have the following (along with the TabItemEx style specified above):
<ig:XamTabControl ItemsSource="{Binding WorkspaceTabNames}" TabStripPlacement="Bottom" ContentTemplate="{StaticResource tabContentDataTemplate}" Theme="Office2k7Blue">
Tried to modify the style of TabItemEx slightly to this:
<igEditors:XamTextEditor PreviewMouseLeftButtonDown="tbEdit_PreviewMouseDoubleClick" BorderThickness="0" Background="Transparent" EditModeStarting="tbEdit_EditModeStarting" EditModeEnding="tbEdit_EditModeEnding" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Header, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
In the view model of my view containing the tab control:
public ObservableCollection<WorkspaceViewModel> WorkspaceTabNames{ get { return new ObservableCollection<WorkspaceViewModel> { new WorkspaceViewModel("Tab 1", "Content 1"), new WorkspaceViewModel("Tab 2", "Content 2"), new WorkspaceViewModel("Tab x", "Content x") }; }}
And finally, the view model itself:
public class WorkspaceViewModel: INotifyPropertyChanged{ public string Header { get; private set; } public string Content { get; private set; }
public WorkspaceViewModel(string header, string content) { Header = header; Content = content; }
#region INotifyPropertyChanged Implementation ... #endregion}
I see a number of problems. First, you are setting the ContentTemplate. The ContentTemplate of a TabControl affects what is showing the content - i.e. the main body of the tab control and not the header of the tab item. You would want to set the ItemTemplate which is what affects the content of the items created by an items control (TabItemEx in this case). Second you are using TemplatedParent as a relativesource for the binding but the templated parent of something within a DataTemplate is the ContentPresenter that uses the DataTemplate and even then it doesn't have a Header property - the DataContext does so you need to remove the RelativeSource from that binding. Third, the Header property has a private setter - if you want to be able to update that then it needs to be public. So something like this:
<Grid xmlns:igWindows="http://infragistics.com/Windows" xmlns:igEditors="http://infragistics.com/Editors"> <Grid.Resources> <DataTemplate x:Key="tabContentDataTemplate"> <igEditors:XamTextEditor BorderThickness="0" Background="Transparent" Value="{Binding Path=Header, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> </DataTemplate> </Grid.Resources> <igWindows:XamTabControl ItemsSource="{Binding WorkspaceTabNames}" TabStripPlacement="Bottom" ItemTemplate="{StaticResource tabContentDataTemplate}" Theme="Office2k7Blue"/> </Grid>
Thanks for your reply but I don't think that was the issue. The editability of the tab is handled by a style (which is in the example link) and not in a DataTemplate.
I got around this by using CAL and adding the views to the xamTabControl region instead of binding my list of viewmodels to the ItemSource of the xamTabControl. Still not sure why this approach worked but for now I do not really have the time to investigate further :-)