Skip to content
This repository has been archived by the owner on May 29, 2024. It is now read-only.

Scroll to selected item #137

Open
vrykov opened this issue Jul 7, 2020 · 3 comments
Open

Scroll to selected item #137

vrykov opened this issue Jul 7, 2020 · 3 comments

Comments

@vrykov
Copy link

vrykov commented Jul 7, 2020

Hello,

first of all, Xamarin.Forms.DataGrid is a great thing, thanks!

I'm struggling with making an element visible. Here's my scenario:
I have a long collection of items. When I set SelectedItem in viewmodel, DataGrid selects that item.
However, if newly selected item wasn't visible before, DataGrid does not scroll to make it visible.

In ListView there is a ScrollTo method with does excatly what I want, for example:

listView.ScrollTo(vm.ItemsCollection.LastOrDefault(), ScrollToPosition.Start, false);

Is this possible with Xamarin.Forms.DataGrid?

@akgulebubekir
Copy link
Owner

Hi, such a feature not exists for now, but would be nice to implement. Will implement when i have free time

@vrykov
Copy link
Author

vrykov commented Jul 7, 2020

Thanks! Really looking forward.

Possible implementation in DataGrid.xaml.cs:

    public void ScrollTo(object item, ScrollToPosition position, bool animated)
    {
        _listView.ScrollTo(item, position, animated);
    }

    public void ScrollTo(object item, object group, ScrollToPosition position, bool animated)
    {
        _listView.ScrollTo(item, group, position, animated);
    }

Then methods can be easily used from codebehind in ItemSelected event handlers:

    private void DataGrid_ItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
        var dg = (Xamarin.Forms.DataGrid)sender;
        dg.ScrollTo(e.SelectedItem, ScrollToPosition.Start, false);
    }

(Not tested yet)

@vrykov
Copy link
Author

vrykov commented Jul 14, 2020

If someone is interested, here's some dirty workaround:

  1. Add this class to your project

using System.Reflection;
using Xamarin.Forms;
using System.Linq;

class DataGridListViewCachingUtil
{
    private ListView _listView;
    public ListView GetListView(Xamarin.Forms.DataGrid.DataGrid grid)
    {
        if (_listView == null)
        {
            var gridType = typeof(Xamarin.Forms.DataGrid.DataGrid);

            FieldInfo[] fields = gridType.GetFields(
                         BindingFlags.NonPublic |
                         BindingFlags.Instance);

            var _lv = fields.FirstOrDefault(i => i.Name == "_listView");
            if (_lv != null)
            {
                _listView = (ListView)_lv.GetValue(grid);
            }
        }

        return _listView;
    }

    public ListView GetListView(object sender)
    {
        return GetListView((Xamarin.Forms.DataGrid.DataGrid)sender);
    }
}
  1. This is an example showing how listview can be accessed from codebehind:

     private void DataGrid_ItemSelected(object sender, SelectedItemChangedEventArgs e)
     {
         if(e.SelectedItemIndex != -1)
         {
             var listView = cachingUtil.GetListView(sender);
             if (listView != null)
                 listView.ScrollTo(e.SelectedItem, ScrollToPosition.Start, false);
         }
     }
    

ListView object is cached within the utility class.
Works for me if DataGrid is visible.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants