AvaloniaUI / Avalonia.Controls.TreeDataGrid

A combined TreeView/DataGrid for Avalonia.
MIT License
264 stars 54 forks source link

[Feature request] Scroll into View #133

Open SuperJMN opened 1 year ago

SuperJMN commented 1 year ago

Request

Provide a way to bring a given item into view.

Why?

When we need to highlight an item, E. g. a search feature, we'd need to bring the item into view so the user can locate the item easily. This will involve scrolling to the desired item.

This is intrinsically complex due to the nature of the TreeDataGrid which allows hierarchies. Bringing a model into view would imply to know which parents to expand in the hierarchy.

cc @grokys just in case you know an easy way to do this.

elix22 commented 1 year ago

+1 Would be a great addition. I am trying to figure out how todo it . So far didn't find any effective way (probably due to my unfamiliarity of this code , hopefully will change in the future :) ).

P.S. Thanks for this great control.

danipen commented 1 year ago

@miryamGSM are we already doing this ourselves in our codebase, right? We maybe can share how we're doing it for reference.

SuperJMN commented 1 year ago

@elix22 Feel free to try my code:

https://github.com/SuperJMN-Zafiro/Zafiro.Avalonia/blob/master/Zafiro.Avalonia/ScrollToSelectedItemBehavior.cs

Sample usage here: https://github.com/SuperJMN-Zafiro/Zafiro.Avalonia/blob/ec75ea0defde8ed8d39620ef9ee32dc44842ba21/DataGridSample/Views/MainWindow.axaml#L22

SuperJMN commented 1 year ago

@danipen It would be nice if you can share the code.

elix22 commented 1 year ago

@SuperJMN thanks for sharing I tried it , unfortunately it doesn't work in my case This one is being called ScrollToSelectedItemBehavior<T> :OnAttachedToVisualTree()

However when I select one of the items , it doesn't scroll I am going on a short vacation till next week ,once I will be back I will try to debug it .

Thanks

elix22 commented 1 year ago

@SuperJMN It works !! I had a typo in this line , didn't point to my model , fix it and it works like a charm . <avalonia:ScrollToSelectedItemBehavior x:TypeArguments="m:DragDropItem">

Thanks alot

SuperJMN commented 1 year ago

@SuperJMN It works !! I had a typo in this line , didn't point to my model , fix it and it works like a charm . <avalonia:ScrollToSelectedItemBehavior x:TypeArguments="m:DragDropItem">

Thanks alot

That's so nice to hear! Once https://github.com/AvaloniaUI/Avalonia.Controls.TreeDataGrid/pull/134 gets merged, the usage will be quite more straightforward.

elix22 commented 1 year ago

@SuperJMN Some feedback It works as expected if only 1 item is selected But won't work for multiple selections , because your code refers only to the first selection (even if there are multiple selections) https://github.com/SuperJMN-Zafiro/Zafiro.Avalonia/blob/master/Zafiro.Avalonia/ScrollToSelectedItemBehavior.cs#L27

So if I will call multiple timessource.RowSelection!.Select(index); , It will always scroll to the first selection

danipen commented 1 year ago

Well, we basically do the following:

  1. Calculate the IndexPath for the node model.
  2. Calculate the RowIndex using ModelIndexToRowIndex.
  3. We call BringIntoView for the RowsPresenter.
    var node = /* the model node */;
    IndexPath modelIndex = FindModelIndex(_treeDataGrid, node);
    int rowIndex = _treeDataGrid.Rows.ModelIndexToRowIndex(modelIndex);
    _treeDataGrid.RowsPresenter?.BringIntoView(rowIndex);
elix22 commented 1 year ago

@danipen Will you handle ?

  1. Multiple selections
  2. Expanding the hierarchy

    BringIntoView implemented by @SuperJMN handles #2 https://github.com/SuperJMN-Zafiro/Zafiro.Avalonia/blob/master/Zafiro.Avalonia/TreeDataGridMixin.cs#L40