Search code examples
wpfdatagriddatagridrowheader

DataGrid Placeholder Row Header customization


I have a WPF DataGrid, the DataSource is an ObservableCollection<MyItem>

MyItem has property called Hour, which is automatically generated. Hour is used as a RowHeader in the DataGrid.

If I add a new item to the collection, the Hour property is filled and the RowHeader will display it. But I have no control over the RowHeader of the "placeholder row".

How can I achieve that the RowHeader of the placeholder row is always a * character, but all the normal rows have the Hour property as a RowHeader?


Solution

  • Removed hour from MyItem (not really needed there), and made RowHeader to be auto-generated. If Row.Item is not a MyItem then the * gets set to Row.Header programatically.

        void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            if (e.Row.Item is MyItem)
            {
                e.Row.Header = (e.Row.GetIndex()).ToString("D2");
            }
            else // Row is {NewItemPlaceholder}
            {
                e.Row.Header = "✲";
            }
        }