Avalonia icon indicating copy to clipboard operation
Avalonia copied to clipboard

DataGridRow.Index does not raise PropertyChanged event

Open mrj001 opened this issue 1 year ago • 2 comments

Describe the bug When adding rows to a DataGrid, indices of existing rows may change. However, the Index property is not firing a PropertyChanged event.

To Reproduce Add a new row to a DataGrid at a location other than the end. I have RowHeaders set to visible and bound their content to the Row Index using: <Setter Property="Content" Value="{Binding $parent[DataGridRow].Index}" /> Then, I inserted a row at index 0 of the ObservableCollection being displayed. The new row got the correct row index in its header, but none of the others updated.

Expected behavior Existing row numbers should update, but do not.

Desktop (please complete the following information):

  • OS: Linux (Ubuntu (Orange Pi 1.1.6 Jammy))
  • Version 0.10.21 (but the current source does not appear to raise an event, so I expect it still happens.)

mrj001 avatar Jul 02 '23 14:07 mrj001

Index is internal, you you are not supposed to bind to it directly. See: https://github.com/AvaloniaUI/Avalonia/blob/a182e1fd98edf858970a9efaf5dc24ffa950a41b/src/Avalonia.Controls.DataGrid/DataGridRow.cs#L217-L224

If you need to bind to an index, consider to add this property to your ItemViewModel

timunie avatar Jul 03 '23 11:07 timunie

Adding this as a property to the ItemViewModel seems rather unclean and like a lot of excess work. Inserting an item then requires updating all the other items. Also, if the user sorts on a column, the alteration in display order will be purely in the UI and not the underlying collection. Updating the underlying item index property then seems like a lot of excess work that may be error prone.

In my use case, it is desirable to have a Converter to translate row numbers into the displayed value in the header.

mrj001 avatar Jul 03 '23 14:07 mrj001