title: Dynamic percentage-based width in WPF
body: <p>maybe you guys can help me figure this out: I have a Dictionary and an ItemsControl that is binded to that dictionary. The Key of each entry determines the content of each Item in the ItemsControl and the Value determines the width of each Item. The big problem with this: The width is a percentage value, so it tells me that, for example, my Item needs to be 20% of its parent in size. </p>
  
  <p>How can I achieve this?
  I know Grids are able to work with star-based widths, but since I have to define the GridDefinition at the beginning of the Grid I cannot do this in the ItemsControl.ItemTemplate.</p>
  
  <p>Current code:</p>
  
  <pre><code>&lt;ItemsControl ItemsSource="{Binding Distribution}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"&gt; 
          &lt;ItemsControl.ItemsPanel&gt;
              &lt;ItemsPanelTemplate&gt;
                  &lt;Grid IsItemsHost="True" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" /&gt;
              &lt;/ItemsPanelTemplate&gt;
          &lt;/ItemsControl.ItemsPanel&gt; 
          &lt;ItemsControl.ItemTemplate&gt;
              &lt;DataTemplate&gt;
                  &lt;!-- I NEED THIS TO BE A CERTAIN PERCENTAGE IN WIDTH --&gt;
                  &lt;Label Content="{Binding Key.Text}" Foreground="{Binding Key.Color}"/&gt; 
              &lt;/DataTemplate&gt;
          &lt;/ItemsControl.ItemTemplate&gt;
      &lt;/ItemsControl&gt;
  </code></pre>
  
  <p>Any ideas on this? Is there any elegant way to solve this?</p>
  
  <p>Thanks!</p>
  
  <p><em>Clarifications</em>: The percentage is supposed to be based  on the ItemControls parent!</p>
  
  <p><em>And another one</em>: Each item is supposed to be one column of the grid, not a row. So I need all the items to be next to each other in the same row.</p>
  
  <p><strong>The solution</strong>:</p>
  
  <p>Thanks for your help, this problem can be solved by using Multibinding and Binding to the ActualWidth of the ItemsControl. This way, whenever the ItemsControl changes in size, the Items change as well. A Grid is not needed. This solution only creates a relative width, but the same solution can of course be applied to the height of the items. This is a short version, for a more thorough explanation see down below:</p>
  
  <p><em>XAML</em>:</p>
  
  <pre><code>&lt;ItemsControl ItemsSource="{Binding Distribution}" Name="itemsControl"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch"&gt;
           &lt;ItemsControl.ItemsPanel&gt;
              &lt;ItemsPanelTemplate&gt;
                  &lt;StackPanel IsItemsHost="True" Orientation="Horizontal" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" /&gt;
              &lt;/ItemsPanelTemplate&gt;
          &lt;/ItemsControl.ItemsPanel&gt; 
          &lt;ItemsControl.ItemTemplate&gt;
              &lt;DataTemplate&gt;
                  &lt;Label Content="{Binding Key.Text}" 
                         Foreground="{Binding Key.Color}"&gt;
                      &lt;Label.Width&gt;
                          &lt;MultiBinding Converter="{StaticResource myConverter}"&gt;
                              &lt;Binding Path="Value"/&gt;
                              &lt;Binding Path="ActualWidth" ElementName="itemsControl"/&gt;
                          &lt;/MultiBinding&gt;
                      &lt;/Label.Width&gt;
                  &lt;/Label&gt;  
              &lt;/DataTemplate&gt;
          &lt;/ItemsControl.ItemTemplate&gt;
      &lt;/ItemsControl&gt;
  </code></pre>
  
  <p><em>Converter</em>:</p>
  
  <pre><code>class MyConverter : IMultiValueConverter
  {
      public object Convert(object[] value, Type targetType, object parameter, CultureInfo culture)
      {
          //[1] contains the ItemsControl.ActualWidth we binded to, [0] the percentage
          //In this case, I assume the percentage is a double between 0 and 1
          return (double)value[1] * (double)value[0];
      }
  
      public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
      {
          throw new NotImplementedException();
      }
  }
  </code></pre>
  
  <p>And that should do the trick!</p>