Data binding without XAML?

ysteinSelbekkysteinSelbekk NOMember, University

Hi, I have been trying to understand the MVVM pattern looking at data binding using Xamarin.Forms ListView.
What I can't get to work is to actually see the data when populating it on a button press. What I have is this:

customerListView = new ListView { ItemsSource = customers, ItemTemplate = new DataTemplate (() => { Label customerNameLabel = new Label(); customerNameLabel.SetBinding(Label.TextProperty, "Name"); StackLayout listView = new StackLayout (); listView.Children.Add (customerNameLabel); return new ViewCell { View = listView }; }) };

customers is a List where the CustomerModel class implements the INotifyPropertyChanged
Where I was hoping this would work.

private string name; public string Name { get { return name; } set { name = value; OnPropertyChanegd (this, "Name"); } } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanegd (object sender, string propertyName) { if (this.PropertyChanged != null && propertyName != String.Empty) { PropertyChanged (sender, new PropertyChangedEventArgs (propertyName)); } }

My main question is - is this at all possible without using XAML?

Best Answer


  • ysteinSelbekkysteinSelbekk NOMember, University

    Yeah that worked :smile: Changing List<> to ObservableCollection<>
    Thanks :smiley:

Sign In or Register to comment.