title: Automatically resize tab headers based on the count of tabs (AvaloniaUI)
body: <p>I want to implement styling for TabControl in my browser like in chrome. The number of tabs is not fixed, so I want the tab headers to decrease depending on the number of tabs.</p>
  <p>To do this, I bind the width of the Grid in the DataTemplate to the count of tabs, which is passed to the converter, which returns the actual width.</p>
  <p>But for some reason this does not work, I know that the TabItemCount is accurately passed to the converter. If the converter returns a fixed value, then nothing prevents the tabs from resizing</p>
  <p>Style:</p>
  <pre><code>&lt;Style Selector=&quot;TabControl&quot;&gt;
          &lt;Setter Property=&quot;BorderThickness&quot; Value=&quot;0&quot;&gt;&lt;/Setter&gt;
          &lt;Setter Property=&quot;ItemsPanel&quot;&gt;
              &lt;ItemsPanelTemplate&gt;
                  &lt;StackPanel Orientation=&quot;Horizontal&quot; 
                              Height=&quot;50&quot;
                              Margin=&quot;10,-5,-10,5&quot;&gt;&lt;/StackPanel&gt;
              &lt;/ItemsPanelTemplate&gt;
          &lt;/Setter&gt;
          &lt;Setter Property=&quot;ContentTemplate&quot;&gt;
              &lt;DataTemplate DataType=&quot;vm:WebsiteTabVM&quot;&gt;
                  &lt;views:WebsiteTab&gt;&lt;/views:WebsiteTab&gt;
              &lt;/DataTemplate&gt;
          &lt;/Setter&gt;
          &lt;Setter Property=&quot;ItemTemplate&quot;&gt;
              &lt;DataTemplate DataType=&quot;vm:TabVM&quot;&gt;
                  &lt;Grid Width=&quot;{Binding DataContext.TabItemCount, 
                               Converter={StaticResource CountToWidthConverter},
                               RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabControl}}}&quot;
                        HorizontalAlignment=&quot;Center&quot; VerticalAlignment=&quot;Center&quot;
                        Margin=&quot;5&quot;&gt;
  
                        &lt;!--ItemTemplate--&gt;
  </code></pre>
  <p>CountToWidthConverter:</p>
  <pre><code>using Avalonia.Data.Converters;
  using System;
  
  namespace SimpleBrowser.Helpers
  {
      public class CountToWidthConverter : IValueConverter
      {
          public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
              return 1000 / (int)value;
          }
  
          public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
              throw new NotImplementedException();
          }
      }
  }
  </code></pre>
  <p>MainVM, which has a count of tabs property:</p>
  <pre><code>using Avalonia.Controls;
  using Microsoft.Toolkit.Mvvm.Input;
  using ReactiveUI;
  using System.Collections.Generic;
  using System.Collections.ObjectModel;
  using System.Windows.Input;
  using System.Linq;
  
  namespace SimpleBrowser.ViewModels
  {
      public class MainVM : ViewModelBase
      {
          public MainVM()
          {
              TabVMs = new ObservableCollection&lt;TabVM&gt;();
              TabVMs.Add(new WebsiteTabVM());
              TabVMs.Add(new WebsiteTabVM() {Name=&quot;Test tab withbigtext&quot;});
              TabVMs.Add(new WebsiteTabVM());
              TabVMs.Add(new WebsiteTabVM());
  
              SortTabs();
          }
          private ObservableCollection&lt;TabVM&gt; _tabVMs;
          public ObservableCollection&lt;TabVM&gt; TabVMs
          {
              get { return _tabVMs; }
              set 
              { 
                  this.RaiseAndSetIfChanged(ref _tabVMs, value);
                  TabItemCount = TabVMs.Count;
              }
          }
  
          private double _tabItemCount;
          public double TabItemCount
          {
              get { return _tabItemCount; }
              set { this.RaiseAndSetIfChanged(ref _tabItemCount, value); }
          }
  
          #region Command
          private ICommand _addNewTab;
          public ICommand AddNewTab
          {
              get
              {
                  return _addNewTab ??= new RelayCommand(() =&gt;
                  {
                      TabVMs.Add(new WebsiteTabVM());
                      SortTabs();
                  });
              }
          }
          private ICommand _removeTab;
          public ICommand RemoveTab
          {
              get
              {
                  return _removeTab ??= new RelayCommand&lt;int&gt;(obj =&gt;
                  {
                      TabVMs.RemoveAt(obj);
                      SortTabs();
                  });
              }
          }
          #endregion
  
          public void SortTabs()
          {
              for (int i = 0; i &lt; TabVMs.Count; i++)
              {
                  TabVMs[i].Index = i;
                  TabVMs[i].IsTabLast = false;
              }
              TabVMs.Last().IsTabLast = true;
              TabItemCount = TabVMs.Count;
          }
      }
  }
  </code></pre>
  <p>If you need more code, I will definitely provide you with it.
  Thanks for considering my request</p>