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.

Collection View Binding is not getting updated on page load

Mahesh453261Mahesh453261 Member ✭✭
edited October 26 in Xamarin.Forms

Hi,

Greetings!

I have a checkbox which should be checked With selected values from the DB on Page Load. I have azure mobile Service client in backend and after the DB call, I am updating the observable collection binded to the Collection view that contains the check box. But on Page load check box is not getting checked as the query returns the result lately. when I keep the breakpoint before load for couple of seconds then the page loads with checkbox checked.

How do I add the delay or bind the collection appropriately to have the desired result.

Code which is marking the Is checked to True.

public ItemUpdate()
        {
            InitializeComponent();
        GetCheckboxAsync();
            listview.ItemsSource = ivm.ItemValues;
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();
            BindingContext = ivm;

        }
public async void GetCheckboxAsync()
        {

            Itemcat = await App.client.GetTable<Item_Categories>().Where(x => x.HotelId == App.HotelId && x.ItemName == Item.ItemName).ToListAsync();

            foreach (var itemcategoryname in Itemcat)
            {
                int i = 0;
                foreach (var item in ItemValue)
                {
                    item.ItemName = itemcategoryname.ItemName;
                    item.id = itemcategoryname.id;

                    if (item.ItemCategory == itemcategoryname.ItemCategory)
                    {
                       ItemValue[i].IsChecked = true;
                    }
                    i = i + 1;
                }
            }
        }

Observable collection:

public ObservableCollection<Item_Categories> _ItemValue = new ObservableCollection<Item_Categories>();
        public ObservableCollection<Item_Categories> ItemValue
        {
            get
            {
                return _ItemValue;

            }
            set
            {
                if (_ItemValue != value)
                {
                    _ItemValue = value;
                }
                NotifyPropertyChanged();
            }
        }

Above Observable collection is bonded to collection view of the checkbox

CollectionView x:Name="listview" ItemsSource="{Binding ItemValue}"

CollectionView x:Name="listview" ItemsSource="{Binding ItemValue}"
                        CollectionView.ItemTemplate
                            DataTemplate
                                    StackLayout Orientation="Horizontal" Padding="5,0,5,0"
                                        CheckBox x:Name="CheckBoxValue"  Color="White" BackgroundColor="YellowGreen"  HeightRequest="40"
                                                  IsChecked="{Binding IsChecked}" CheckedChanged="CheckBox_CheckedChanged"
                                        Label Text="{Binding ItemCategory}" HorizontalOptions="StartAndExpand"
                                            Label.GestureRecognizers
                                                TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"
                                            Label.GestureRecognizers
                                            Label
                                    StackLayout
                            DataTemplate
                        CollectionView.ItemTemplate
 CollectionView

Regards,
Mahesh B

Best Answer

Answers

  • Mahesh453261Mahesh453261 Member ✭✭

    Thanks a ton @jezh It worked a like a champ :). I am actually developing an app and a beginner in Xamarin. Please see in your leisure time if you can help with the below one
    https://forums.xamarin.com/discussion/185788/crossmedia-plugin-is-calling-async-twice-and-app-crashes/p1?new=1

    Thanks again :)

Sign In or Register to comment.