My CollectionView not update ItemSources ussing MVVM?

Liem Nguyen 91 Reputation points
2024-06-22T08:08:36.86+00:00

Hi, my name Liem!

I using CommunityToolkit.MVVM for my NET MAUI project.

I have success load data to CollectionView by binding ItemSource to [ObservableProperty] ObservableColleciont<T> Items.

Now I use for loop to change item properties inside Items but the CollectionView not update.

This is my code:

[ObservableProperty]

string statusColor;

[ObservableProperty]
string isOn;

[ObservableProperty] 
ObservableColleciont<T> Items;



public ScanPageViewModel()

{

    Items = new ObservableCollection<IPAddressModel>();

    loadGroup();

}

Change value method


private async void ChangeValue()
{
    if (Items == null) return;

    for(int i = 0; i < Items.Count - 1; i++)

    {

        if (Items[i].Address != null)

        {

            await Task.Run(async () =>

            {

                isUp = isAddressAvailable(Items[i].Address);
                if (isUp == true)

                {

                    //Address Available

                    StatusColor = "Green";

                    IsOn = "Address Available";

                    Items[i].IsOn = IsOn;

                    Items[i].StatusColor = StatusColor;

                }

                else

                {

                   //Address NOT Available

                    StatusColor = "Red";

                    IsOn = " Address NOT Available";

                    Items[i].IsOn = IsOn;

                    Items[i].StatusColor = StatusColor;                           `

                }

            });                    

        }

    }

}

Thank you!

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,606 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 76,551 Reputation points Microsoft Vendor
    2024-06-24T05:57:15.1466667+00:00

    Hello,

    Based on your ChangeValue method, you need to update the items in the CollectionView, please move your statusColor , isOn property to your IPAddressModel and add [ObservableProperty] like following code.

    public partial class IPAddressModel: ObservableObject
    {
         [ObservableProperty]
         private string? address;
     
         [ObservableProperty]
         private string? isOn;
      
         [ObservableProperty]
         private string? statusColor;
     
    }
    

    After that, you can control the statusColor and isOn value for each item at the runtime. If you add the statusColor and isOn property in the ViewModel, it will change the all the items' content.

    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.

    1 person found this answer helpful.

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.