Net Maui calender view how to make visible current date on collectionview scroll position ?

Sami 966 Reputation points
2023-04-28T07:36:32.44+00:00
  It is not working like that  

private void ListDates_BindingContextChanged(object sender, EventArgs e) {
        var lst = (ObservableCollection<DateItem>)((CollectionView)sender).ItemsSource;
        lblMonth.Text = lst.First().Year + " " + lst.First().Month;
        MyBindingList = lst;
        //var items = newValue as ObservableCollection<DateItem>;
        //var control = (CalendarView)bindable;
        var index = lst.ToList().FindIndex(p => p.Selected);
        if (index > -1)
            listDates.ScrollTo(index, -1, ScrollToPosition.MakeVisible, true);
    }
Developer technologies | .NET | .NET MAUI
{count} votes

Accepted answer
  1. Anonymous
    2023-05-02T07:11:17.94+00:00

    Hello,

    You can add a Task.Delay before executing the scroll event in the ListDates_BindingContextChanged method, here is my edited code.

    private async void ListDates_BindingContextChanged(object sender, EventArgs e) {
            var lst = (ObservableCollection<DateItem>)((CollectionView)sender).ItemsSource;
            lblMonth.Text = lst.First().Year + " " + lst.First().Month;
            MyBindingList = lst;
            //var items = newValue as ObservableCollection<DateItem>;
            //var control = (CalendarView)bindable;
            var index = lst.ToList().FindIndex(p => p.Selected);
           if (index > -1)
            {
                await Task.Delay(500);
                listDates.ScrollTo(index, -1, ScrollToPosition.MakeVisible, true);
            }
        }
    
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.