title: ListView Column Width Auto
body: <p>I wrote code in XAML - WPF Browser Application - Page , just one <code>Listview</code> and one <code>button</code> to add new data to the <code>listview</code> (from other file), I am trying to make the first column auto size itself when the button is pushed, I am using Visual Studio c# 2010.</p>
  
  <p>I've used the following method in the code behind, but <code>AutoResizeColumns</code> won't be recognized and gives an error.</p>
  
  <p>Unfortunately, none of the previously suggested solutions worked with me.</p>
  
  <p>The Code Behind </p>
  
  <pre><code>public partial class Page1 : Page, INotifyPropertyChanged
  {
  
      public Page1()
      {
  
          InitializeComponent();
          this.DataContext = new Page1Model();
  
      }
  
      private void TestListe_SelectionChanged(object sender, SelectionChangedEventArgs e)
      {
      }
  
      public event PropertyChangedEventHandler PropertyChanged;
  
      private void Button1_Click(object sender, RoutedEventArgs e)
  
     {
          TestListe1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
     }
  
  }
  </code></pre>
  
  <p>XAML </p>
  
  <pre><code>&lt;Grid&gt;
  
      &lt;Grid.RowDefinitions&gt;
          &lt;RowDefinition Height="3*"/&gt;
          &lt;RowDefinition Height="1*"/&gt;
      &lt;/Grid.RowDefinitions&gt;
      &lt;ListView Name="TestListe1"   Margin="68,22,421,8"  FontSize="12"  &gt;
  
          &lt;ListView.View&gt;
              &lt;GridView&gt;
                  &lt;GridView.Columns&gt;
                        &lt;GridViewColumn Width="auto"&gt;                                &lt;GridViewColumn.HeaderTemplate&gt;
                              &lt;DataTemplate&gt;
                                  &lt;TextBlock Text="ST1" Margin="10,0,10,1"/&gt;
                              &lt;/DataTemplate&gt;
                          &lt;/GridViewColumn.HeaderTemplate&gt;
                          &lt;GridViewColumn.CellTemplate&gt;
                              &lt;DataTemplate&gt;
                                  &lt;TextBlock Text="{Binding One}" /&gt;
                              &lt;/DataTemplate&gt;
                          &lt;/GridViewColumn.CellTemplate&gt;
                      &lt;/GridViewColumn&gt;
  
                                          &lt;/GridView.Columns&gt;
              &lt;/GridView&gt;
          &lt;/ListView.View&gt;
     &lt;/ListView&gt;
  
      &lt;Button Name="Button1" Grid.Row="1" Height="27" Width="95" Margin="262,24,444,74"  Click="Button1_Click" /&gt;
  
  &lt;/Grid&gt;
  </code></pre>