Using the MVVM template, I have a Model, ViewModel and View that contains a ListView. The ListView is bound to the ViewModel, which is an ObservableCollection of the Model class. I can make the binding for the initial display work and can update the properties of the Model class for the corresponding row when it affects the view, but I canβt get the view for the update by pulling data from the Model class into an ObservableCollection. The ListView class does not contain a method for canceling or force updating, which would solve my problem. How to get a ListView to update after updating the data on the model?
Here is a simple example of what I'm trying to do: Each line contains a button and a shortcut. After clicking the button, I can update the shortcut that will be displayed on the screen. I need to update the model, which, in turn, must force update the view. However, I cannot get this to work. In a real application, the model will be updated at the level of business logic, and not in the view, after which I need to force the ListView to be updated.
Code example:
using System; using System.Collections.ObjectModel; using Xamarin.Forms; namespace ListViewTest { public class Model { public static int ids = 0; public Model(string count) { Id = +1; Count = count; } public int Id { get; set; } public string Count { get; set; } } public class ModelList : ObservableCollection<Model> { } public class ViewModel { ModelList list = new ModelList(); public ModelList ViewModelList { get { return list; } set { list = value; } } } public partial class MainPage : ContentPage { public ViewModel viewModel; public class DataCell : ViewCell { public DataCell() { var Btn = new Button(); Btn.Text = "Click"; var Data = new Label(); Data.SetBinding(Label.TextProperty,"Count"); Btn.Clicked += (object sender, EventArgs e) => { Model model = (Model)(((Button)sender).Parent.BindingContext); int count = Convert.ToInt32(model.Count); count++; model.Count = count.ToString();
Ken k source share