DataGrid issue when defining both general RowHeight and individual DataGridRow.Height values

We’ve just encountered a strange row height-related issue when using DataGrid control from WPF 4: if you set both DataGrid.RowHeight and individual DataGridRow.Height values for specific rows (to get some larger items), the output is not as expected:

<DataGrid Name=”DataGrid” RowHeight=”25
          LoadingRow=”DataGrid_LoadingRow”/>

DataGrid.ItemsSource = new ObservableCollection<string> {
    “abcdef”,
    “abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ“,
    “abcdefghijkl” };

void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
    var item = e.Row.DataContext as string;
    if (item != null && item.Length > 25)
        e.Row.SetBinding(DataGridRow.HeightProperty,
                         new Binding(“Length”));
}

image

Specifically, as you can see in the screenshot, the second row has height set to 52, but the grid lines indicate that the row presenter actually uses only the standard 25 pixels. (Actually, the remaining space is empty!) Instead, you would probably expect that the entire available space of 52 pixels would be used by the row presenter, generating continuous vertical grid lines. Row or cell styling doesn’t seem to help: VerticalAlignment properties are already set to Stretch internally.

To resolve this issue, a possible workaround would be, however, to let DataGrid.RowHeight not set (or set it to Auto/double.NaN) and make sure that each individual DataGridRow.Height is set to a fixed value, as required. It’s not very elegant, but it works.

About Sorin Dolha

My passion is software development, but I also like physics.
This entry was posted in Computers and Internet and tagged , , , . Bookmark the permalink.

Add a reply