DataGrid row header width gets bigger, but never smaller?

Feb 16, 2009 at 7: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"
    Title="Window1" Height="300" Width="300">
    <dg:DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Data}" HorizontalAlignment="Left" VerticalAlignment="Top"
                 Name="dgMain" HeadersVisibility="All">
        <dg:DataGridTextColumn Binding="{Binding Name}" />
        <dg:DataGridTextColumn Binding="{Binding Description}" />
        <Style TargetType="{x:Type dg:DataGridRowHeader}">
          <Setter Property="Content" Value="{Binding Name}" />

  /// <summary>
  /// Interaction logic for Window1.xaml
  /// </summary>
  public partial class Window1 : Window
    public Window1()
      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; }
        _Name = value;

    string _Description;
    public string Description
      get { return _Description; }
        _Description = value;

    #region INotifyPropertyChanged Members

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