<CollectionView ItemsSource="{Binding ProductDb}">
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout>
<Label Text="{Binding ItemStatus}" />
<Label Text="{Binding ItemId}" />
<Label Text="{Binding ItemDescription}" />
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
The collectionView is first loaded from the database and wait for a specific event occurring in the code. The ItemStatus for all Item data property initially has the same message "Still in progress".
Then later when an event occurs it has to update the ItemStatus for a specific Item Data in CollectionView with new message "Completed !"
The event is just a regular C# method with a timer in it.
After 2 minutes it has to updates one ItemStatus property and changes its message to "Completed".
I tried to update ItemStatus from OnPropertyChanged of INotifyPropertyChanged but it updates all the ItemStatus for each Item data with the same value.
I know it's normal behaviour because ItemStatus property name find itself within each Item data.
I also tried to use x:Name for ItemStatus Label but it doesn't work; and I read on Microsoft web site that DataTemplate creates a new scope for all the elements inside of it,
I was also thinking about creating dynamic elements but not sure if it's gonna work.
By the way, I want to dynamically only update ItemStatus for specific ItemId/Item Data,
I remember in HTML/CSS/Javascript there is kind of CLASS and ID element that you can access from javascript method anywhere in the page.
Please does anybody have an idea how to achieve it?
private string itemStatus = "Still in progress";
public string ItemStatus
{
get => itemStatus;
set
{
if (value == itemStatus)
return;
itemStatus = value;
OnPropertyChanged(nameof(ItemStatus));
}
}
private void UpdateUIItemStatusMessage(string uniqueMessage)
{
ItemStatus = $"Completed !!!";
}
Requested code by Jason
namespace WarehouseMobile
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ProductPage : ContentPage
{
private string itemStatusMessage = "Still in progress";
public string ItemStatus
{
get => itemStatusMessage;
set
{
if (value == itemStatusMessage)
return;
itemStatusMessage = value;
OnPropertyChanged(nameof(ItemStatus));
}
}
private ObservableCollection<ProductDb> _products;
public ObservableCollection<ProductDb> Products
{
get { return _products; }
set
{
_products = value;
OnPropertyChanged(nameof(Products));
}
}
public ProductPage()
{
InitializeComponent();
BindingContext = this;
Products = new ObservableCollection<ProductDb>();
}
protected override async void OnAppearing()
{
base.OnAppearing();
Products = new ObservableCollection<ProductDb>(data);
myCollection.ItemsSource = Products;
}
private void UpdateUIItemStatusMessage(string itemId)
{
var myItems = myCollection.ItemsSource as ObservableCollection<ProductDb>;
var selectedItem = myItems.FirstOrDefault(o => o.Id == 4);
selectedItem.ItemStatus = $"NEW MESSAGE !!! - Completed";
}
}
}
public class ProductDb
{
[PrimaryKey, AutoIncrement]
public int ItemId { get; set; }
public string ItemDescription { get; set; }
public string ItemStatus { get; set; }
}

If you want to update the property of special item of the data list bind to
CollectionView, you need to implement interfaceINotifyPropertyChangedfor your item model(ProductDb.cs).Secondly,it is recommended that you use MVVM to implement your function.
And I have achieved this function,you can refer to the following code:
ProductDb.cs
ProductViewModel.cs
ProductPage.xaml.cs
ProductPage.xaml