Hello,
Welcome to our Microsoft Q&A platform!
New or changed orders are marked as unread. With the loop I want to achieve that only the displayed orders can be marked as read.
You could create a bool property in the model class to mark the item as read. And try to create two ObservableCollection<TestModel>
in the viewModel class. One is used to save the data that is read from database, and the other is the 'read' data which will be shown in the listView.
Check the code:
public class TestViewModel
{
public ObservableCollection<TestModel> DataCollection { get; set; } //store the data of database
public ObservableCollection<TestModel> DisplayDataCollection { get; set; } //filter the data and display the data in the listview
public TestViewModel()
{
DataCollection = new ObservableCollection<TestModel>();
DisplayDataCollection = new ObservableCollection<TestModel>();
DataCollection.CollectionChanged += DataCollection_CollectionChanged;
//add data
}
//this event will be called when you add or remove items from the data collection
private void DataCollection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
foreach (TestModel item in e.NewItems)
{
item.PropertyChanged += Item_PropertyChanged;
}
DisplayDataCollection.Clear();
foreach (var item in DataCollection)
{
if (item.IsRead)
{
DisplayDataCollection.Add(item);
}
}
}
//this event will be called when you modify the items of the data collection
private void Item_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
DisplayDataCollection.Clear();
foreach (var item in DataCollection)
{
if (item.IsRead)
{
DisplayDataCollection.Add(item);
}
}
}
}
Set data binding for the listview.
//page.xaml
<ListView ItemsSource="{Binding DisplayDataCollection}">
...
</ListView>
//page.xaml.cs
public partial class TestPage : ContentPage
{
TestViewModel viewModel;
public Page3()
{
InitializeComponent();
viewModel = new TestViewModel();
BindingContext = viewModel;
}
}
Best Regards,
Jarvan Zhang
If the response is helpful, please click "Accept Answer" and upvote it.
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.