title: Dynamically reducing FontSize to avoid overflow
body: <p>I have written a toy WPF application with a <code>Button</code> and an <code>ItemsControl</code>. Each time you click the <code>Button</code>, the string "AnotherWord" gets added to the <code>ItemsControl</code>. Now, the <code>ItemsControl</code> is displayed as horizontally oriented <code>StackPanel</code> with a fixed width (500 pixels). This means that when you click the button a certain number of times (actually six times), the newly added string gets clipped, like this: </p>
  
  <p>"AnotherWord AnotherWord AnotherWord AnotherWord AnotherWord AnotherWo"</p>
  
  <p>This happens when the <code>FontSize</code> is 13; if you lower it to 12.7 then there's room for the sixth occurence of "AnotherWord". My question is: Is there a way to make this adjustment at runtime so that you avoid the overflow?</p>
  
  <p>EDIT:</p>
  
  <p>In the context of the question, the fixed width of the <code>StackPanel</code> is obligatory - we cannot use more than the 500 pixels we have. Another requirement that the font must never become bigger than 13.</p>
  
  <p>Here is all the code I wrote:</p>
  
  <pre><code>&lt;!-- MainWindow.xaml --&gt;
  &lt;Window x:Class="FontSize.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          Title="MainWindow" Height="350" Width="525"
           DataContext="{Binding RelativeSource={RelativeSource Self}}"&gt;
      &lt;Window.Resources&gt;
          &lt;DataTemplate x:Key="labelTemplate"&gt;
              &lt;Label FontSize="13" Content="AnotherWord"&gt;&lt;/Label&gt;
          &lt;/DataTemplate&gt;
          &lt;ItemsPanelTemplate x:Key="panelTemplate"&gt;
              &lt;StackPanel Orientation="Horizontal" Width="500" Height="50" /&gt;
          &lt;/ItemsPanelTemplate&gt;
      &lt;/Window.Resources&gt;
      &lt;Grid&gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height="Auto" /&gt;
              &lt;RowDefinition Height="Auto" /&gt;
          &lt;/Grid.RowDefinitions&gt;
              &lt;ItemsControl Grid.Row="0" ItemsSource="{Binding Path=MyStrings}" ItemTemplate="{StaticResource labelTemplate}" 
                  ItemsPanel="{StaticResource panelTemplate}" /&gt;
          &lt;Button Grid.Row="1"  Click="Button_Click"&gt;&lt;/Button&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  
  // MainWindow.xaml.cs
  using System.Collections.ObjectModel;
  using System.Windows;
  
  namespace FontSize
  {
      public partial class MainWindow
      {
          public MainWindow()
          {
              InitializeComponent();
              MyStrings = new ObservableCollection&lt;string&gt;();
          }
  
          public ObservableCollection&lt;string&gt; MyStrings
          {
              get { return (ObservableCollection&lt;string&gt;) GetValue(MyStringsProperty); }
              set { SetValue(MyStringsProperty, value); }
          }
  
          private static readonly DependencyProperty MyStringsProperty =
              DependencyProperty.Register("MyStrings", typeof (ObservableCollection&lt;string&gt;), typeof (Window));
  
          private void Button_Click(object sender, RoutedEventArgs e)
          {
              MyStrings.Add("AnotherWord");
          }
      }
  }
  </code></pre>