Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

BindingContext Doesnt update my scrollView

MasterOfXamarinMasterOfXamarin Member ✭✭✭

Hi, I have somtehing like calendar, this calendar loads on button click, but after this click I want to scroll this calendar to the end of this loaded list, but it doesnt update, and if I had 20 items i still have 20 not 27. I have 27 after next click.

 if (calender.ScrollX < 10 && CrossConnectivity.Current.IsConnected)
            {
                var position = calender.ScrollX;
                var Length = calender.ContentSize.Width;
                var viewModel = (CalendarViewModel)BindingContext;
                if (viewModel.LoadLeftList.CanExecute(null))
                {
                    await Task.Run(() => viewModel.LoadLeftList.Execute(null));
                }

                var lastStack = (StackLayout)calendarStackList.Children[0];

                await calender.ScrollToAsync(lastStack, ScrollToPosition.Start, false);

                var mainStackDate = (StackLayout)calendarStackList.Children[_indexOfFrame + MainConsts.GetNumberOfDates];
                var mainFrame = (Frame)mainStackDate.Children.First();
                mainFrame.BackgroundColor = Color.FromHex("#4074DD");

                _lastClickedFrame = mainFrame;
            }
private async void LeftList()
        {
            if (!CrossConnectivity.Current.IsConnected)
            {
                return;
            }

            try
            {
                var datesSet = await MeetingService.GetMeetingsStatusesForDays(new GetMeetingsStatusesForDaysApi
                {
                    Date = CalendarDates.First().EntireDate,
                    Days = MainConsts.GetNumberOfDates,
                    GetDateType = GetDateType.Left
                });

                CalendarDates = datesSet.Concat(CalendarDates).ToList();
                RaisePropertyChanged(nameof(CalendarDates));
            }
            catch (Exception e)
            {
                await LogService.LogAsync(e);
            }           
        }

So if you can see await Task.Run(() => viewModel.LoadLeftList.Execute(null)); this loads my list, but calendarStackList.Children is still same number, how to handle this ?

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    What kind of calendar are you using?

    but it doesnt update

    How did you update your list?
    Could you please share a demo here? We need to reproduce your environment to do some further tests.

  • MasterOfXamarinMasterOfXamarin Member ✭✭✭

    @LandLu this is my "calendar"

     <ScrollView
                    x:Name="calender"
                    Grid.Row="3"
                    Margin="0"
                    Padding="0,20,0,10"
                    BackgroundColor="#0249D7"
                    HorizontalScrollBarVisibility="Never"
                    Orientation="Horizontal">
                    <StackLayout
                        x:Name="calendarStackList"
                        Margin="0"
                        Padding="0"
                        BindableLayout.ItemsSource="{Binding CalendarDates}"
                        Orientation="Horizontal">
                        <BindableLayout.ItemTemplate>
                            <DataTemplate>
                                <StackLayout
                                    Margin="0,0,0,5"
                                    Padding="0,0,0,0"
                                    HorizontalOptions="Start">
                                    <Frame
                                        Margin="3,0,3,0"
                                        Padding="3,0,3,0"
                                        BackgroundColor="#0249D7"
                                        CornerRadius="10"
                                        HasShadow="False"
                                        HeightRequest="70"
                                        VerticalOptions="Center"
                                        WidthRequest="35">
                                        <StackLayout>
                                            <Label
                                                HorizontalTextAlignment="Center"
                                                Text="{Binding DayOfWeek}"
                                                TextColor="White" />
                                            <Label
                                                HorizontalTextAlignment="Center"
                                                Text="{Binding Day}"
                                                TextColor="White" />
                                            <StackLayout HorizontalOptions="Center" Orientation="Horizontal">
                                                <abstractions:RoundedBoxView
                                                    BackgroundColor="#FF4343"
                                                    CornerRadius="32"
                                                    HeightRequest="7"
                                                    HorizontalOptions="Center"
                                                    IsVisible="{Binding ContainsRejected}"
                                                    WidthRequest="7" />
                                                <abstractions:RoundedBoxView
                                                    BackgroundColor="#FFB143"
                                                    CornerRadius="32"
                                                    HeightRequest="7"
                                                    HorizontalOptions="Center"
                                                    IsVisible="{Binding ContainsTo_Approved}"
                                                    WidthRequest="7" />
                                                <abstractions:RoundedBoxView
                                                    BackgroundColor="#1CCC04"
                                                    CornerRadius="32"
                                                    HeightRequest="7"
                                                    HorizontalOptions="Center"
                                                    IsVisible="{Binding ContainsApproved}"
                                                    WidthRequest="7" />
                                            </StackLayout>
                                            <StackLayout.GestureRecognizers>
                                                <TapGestureRecognizer
                                                    Command="{Binding BindingContext.GetMeetingFromDayCommand, Source={x:Reference Name=calendarPage}}"
                                                    CommandParameter="{Binding EntireDate}"
                                                    Tapped="OnItemClicked" />
                                            </StackLayout.GestureRecognizers>
                                        </StackLayout>
                                    </Frame>
                                </StackLayout>
                            </DataTemplate>
                        </BindableLayout.ItemTemplate>
                    </StackLayout>
                </ScrollView>
    

    te method which updates my list is written above

  • riricardoriricardo Member ✭✭

    Hey @MasterOfXamarin are you using INotifyPropertyChanged? When you have a binding you need to notify whenever you change a property in order to update the view. Also, are you using a ObservableColletion for the list?

  • MasterOfXamarinMasterOfXamarin Member ✭✭✭

    @riricardo I use List
    ad this method after update

     protected void RaisePropertyChanged(string propertyName)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
    
  • riricardoriricardo Member ✭✭

    @MasterOfXamarin try using ObservableCollection, I had a similar problem when using a list. It seems that when you use a List and update it's content it doesn't update the List. Alternatively try raise the property changed for the list instead of the item.

  • MasterOfXamarinMasterOfXamarin Member ✭✭✭

    @riricardo I have implemented the observable collection, but still it doest update my Xaml before code ends

  • riricardoriricardo Member ✭✭

    @MasterOfXamarin can you share the code? Also what do you mean by before code ends?

Sign In or Register to comment.