0

I have a TabControl where I create tabs dynamically. I am finding it difficult to change the title of the TabItem.

 <TabControl Name="AttorneysTabControl" Grid.Column="2"  Grid.Row="0">
    <TabControl.Resources>
        <DataTemplate x:Key="AttorneyTabHeader">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Names}" Margin="2,0,0,0" FontSize="16" VerticalAlignment="Center" />
                <Button Width="Auto" UseLayoutRounding="False" BorderBrush="Transparent" Background="Transparent" Click="CloseAttorneysTabButtonClick">
                    <Image Source="/images/close-cross-thin-circular-button/close-cross-thin-circular-button16.png" Height="16"></Image>
                </Button>
            </StackPanel>
        </DataTemplate>
        <DataTemplate x:Key="AttorneyTabContent">
            <local:AttorneyDetails></local:AttorneyDetails>
        </DataTemplate>
    </TabControl.Resources>

For each TabItem I set a HeaderTemplate from the TabControl.Resources like this;

newTabItem.HeaderTemplate = (System.Windows.DataTemplate)AttorneysTabControl.FindResource("AttorneyTabHeader");

But I don't know how to change the contents of the TabItem header once the template has been set. I have tried using DataContext for the TabItem if that's the way to do it but it did not work, so that I could just use Binding in the template. That will be a lot easier.

  • You can define `TabControl.ItemContainerStyle` which is `TabItem` and where you can change/bind `Header`. – Sinatr Nov 13 '17 at 15:57
  • Take a look at https://stackoverflow.com/questions/5650812/how-do-i-bind-a-tabcontrol-to-a-collection-of-viewmodels – Jordy van Eijk Nov 13 '17 at 15:59
  • I am using `newTabItem.HeaderTemplate = (System.Windows.DataTemplate)AttorneysTabControl.FindResource("AttorneyTabHeader");` to set the HeaderTemplate. This template is within the TabControl itself. –  Nov 13 '17 at 15:59
  • @JordyvanEijk I can't bind the TabControl to a model view since I already have an existing tab which I use as an index. So I can only add and remove new ones. –  Nov 13 '17 at 16:01
  • That does not work since the TextBlock is part of a template which is added dynamically. This is similar to the answer below. –  Nov 13 '17 at 23:16

1 Answers1

0

You should normally write (first line is your unchanged code):

newTabItem.HeaderTemplate = (System.Windows.DataTemplate)AttorneysTabControl.FindResource("AttorneyTabHeader");
var tabItemData = new TabItemData() { Name="Initial name"} ;
newTabItem.DataContext = tabItemData;

And then once you need to update the tab header:

tabItemData.Name = "New name".

If that didn't work, that'd probably because your TabItemData.Name property doesn't notify of its changes. So make sure that your TabItemData class implements INotifyPropertyChanged and that the Name property notifies. Example:

public class TabItemData : INotifyPropertyChanged
{
  private string name;
  public string Name
  {
    get { return this.name; }

    set 
    {
        if (value != this.name)
        {
            this.name= value;
            NotifyPropertyChanged("Name");
        }
    }
  }

  public event PropertyChangedEventHandler PropertyChanged;

  private void NotifyPropertyChanged(string propertyName)
  {
    if (PropertyChanged != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
  }

}

In case you're lost I suggest reading the Managing data in a WPF application chapter of my Learn WPF MVVM book.

Arnaud Weil
  • 2,324
  • 20
  • 19
  • It does not seem to work. I have created a class `private class TabItemData : INotifyPropertyChanged`. Can I add DataContext to any element and bind a class to that element in WPF? –  Nov 13 '17 at 16:32
  • Yes you can. That's why I have you write "newTabItem.DataContext = tabItemData". You did fine with implementing INotifyPropertyChanged, now just make sure that the Name property in that classe notifies of its changes so that the UI automatically updates when your code changes its value. See my updated answer with notification code. – Arnaud Weil Nov 15 '17 at 07:46