DataGrid row header width gets bigger, but never smaller?

Feb 16, 2009 at 6:36 PM
I have a data grid where my row headers are bound to a property of the row that is being displayed.  If the value of the property changes to something longer (it's a string), the row header gets wider.  However, if the value changes back to something small, the row headers never restore to their original sizes.  Here is a simple example which demonstrates this issue.  The value of the row header is bound to the value in the first column.  If you type into the first column, the column gets wider, okay.  Hit enter, the row header gets wider too, that's good.  Now change the value in the column back to something short and hit enter.  The column does not get narrower, but I'm not concerned about that right now.  The problem is that the row header never gets smaller again.  I managed to work around this in one case by manually setting the DataGrid.RowHeaderWidth to zero and then back to NaN, then calling UpdateLayout, which made the row header get narrow again but left me extra white space to the right of the last column.

Is there anything I can do so that my grid will get bigger when the row headers are big, and then get smaller again when the row headers are small?  I don't want to waste space on the screen!

<Window x:Class="DataGridExample.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:dg="http://schemas.microsoft.com/wpf/2008/toolkit"
    Title="Window1" Height="300" Width="300">
  <Grid>
    <dg:DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Data}" HorizontalAlignment="Left" VerticalAlignment="Top"
                 Name="dgMain" HeadersVisibility="All">
      <dg:DataGrid.Columns>
        <dg:DataGridTextColumn Binding="{Binding Name}" />
        <dg:DataGridTextColumn Binding="{Binding Description}" />
      </dg:DataGrid.Columns>
      <dg:DataGrid.RowHeaderStyle>
        <Style TargetType="{x:Type dg:DataGridRowHeader}">
          <Setter Property="Content" Value="{Binding Name}" />
        </Style>
      </dg:DataGrid.RowHeaderStyle>
    </dg:DataGrid>
  </Grid>
</Window>

  /// <summary>
  /// Interaction logic for Window1.xaml
  /// </summary>
  public partial class Window1 : Window
  {
    public Window1()
    {
      InitializeComponent();
      DataContext = this;
      _Data.Add(new TestData("foo", "bar"));
      _Data.Add(new TestData("baz", "foo"));
      _Data.Add(new TestData("bar", "baz"));
    }

    ObservableCollection<TestData> _Data = new ObservableCollection<TestData>();
    public ObservableCollection<TestData> Data
    {
      get { return _Data; }
    }
  }

  public class TestData : INotifyPropertyChanged
  {
    public TestData(string name, string description)
    {
      Name = name;
      Description = description;
    }

    string _Name;
    public string Name
    {
      get { return _Name; }
      set
      {
        _Name = value;
        OnPropertyChanged("Name");
      }
    }

    string _Description;
    public string Description
    {
      get { return _Description; }
      set
      {
        _Description = value;
        OnPropertyChanged("Description");
      }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;
    void OnPropertyChanged(string prop) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(prop)); }
    #endregion
  }