title: How can I make a WPF combo box have the width of its widest element in XAML?
body: <p>I know how to do it in code, but can this be done in XAML ?</p>
  
  <p>Window1.xaml:</p>
  
  <pre><code>&lt;Window x:Class="WpfApplication1.Window1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="Window1" Height="300" Width="300"&gt;
      &lt;Grid&gt;
          &lt;ComboBox Name="ComboBox1" HorizontalAlignment="Left" VerticalAlignment="Top"&gt;
              &lt;ComboBoxItem&gt;ComboBoxItem1&lt;/ComboBoxItem&gt;
              &lt;ComboBoxItem&gt;ComboBoxItem2&lt;/ComboBoxItem&gt;
          &lt;/ComboBox&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>Window1.xaml.cs:</p>
  
  <pre><code>using System.Windows;
  using System.Windows.Controls;
  
  namespace WpfApplication1
  {
      public partial class Window1 : Window
      {
          public Window1()
          {
              InitializeComponent();
              double width = 0;
              foreach (ComboBoxItem item in ComboBox1.Items)
              {
                  item.Measure(new Size(
                      double.PositiveInfinity, double.PositiveInfinity));
                  if (item.DesiredSize.Width &gt; width)
                      width = item.DesiredSize.Width;
              }
              ComboBox1.Measure(new Size(
                  double.PositiveInfinity, double.PositiveInfinity));
              ComboBox1.Width = ComboBox1.DesiredSize.Width + width;
          }
      }
  }
  </code></pre>