Last active
August 30, 2023 20:34
-
-
Save MitchMilam/b222fdeb27debb4f2190 to your computer and use it in GitHub Desktop.
Dynamic ListView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.ObjectModel; | |
| using Xamarin.Forms; | |
| namespace XamarinFormsDeepDive | |
| { | |
| class ListViewDemo2 : ContentPage | |
| { | |
| class Person | |
| { | |
| public string Name { private set; get; } | |
| public DateTime Birthday { private set; get; } | |
| public Color FavoriteColor { private set; get; } | |
| public Person(string name, DateTime birthday, Color favoriteColor) | |
| { | |
| Name = name; | |
| Birthday = birthday; | |
| FavoriteColor = favoriteColor; | |
| } | |
| }; | |
| private int Count = 1; | |
| public ListViewDemo2() | |
| { | |
| var people = new ObservableCollection<Person> | |
| { | |
| new Person("Abigail", new DateTime(1975, 1, 15), Color.Aqua), | |
| new Person("Bob", new DateTime(1976, 2, 20), Color.Black), | |
| new Person("Cathy", new DateTime(1977, 3, 10), Color.Blue), | |
| new Person("David", new DateTime(1978, 4, 25), Color.Fuschia), | |
| }; | |
| var buttonAdd = new Button | |
| { | |
| Text = " Add ", | |
| HorizontalOptions = LayoutOptions.Center, | |
| VerticalOptions = LayoutOptions.Start, | |
| }; | |
| var buttonRemove = new Button | |
| { | |
| Text = " Remove ", | |
| HorizontalOptions = LayoutOptions.Center, | |
| VerticalOptions = LayoutOptions.Start, | |
| }; | |
| buttonAdd.Clicked += (sender, e) => people.Add(new Person(string.Format("Name {0}", Count++), DateTime.Today, Color.Blue)); | |
| buttonRemove.Clicked += (sender, e) => people.RemoveAt(people.Count - 1); | |
| var listView = new ListView | |
| { | |
| HorizontalOptions = LayoutOptions.FillAndExpand, | |
| VerticalOptions = LayoutOptions.FillAndExpand, | |
| ItemsSource = people, | |
| ItemTemplate = new DataTemplate(() => | |
| { | |
| var nameLabel = new Label(); | |
| var birthdayLabel = new Label(); | |
| var boxView = new BoxView(); | |
| var stack = new StackLayout | |
| { | |
| Padding = new Thickness(0, 5), | |
| Orientation = StackOrientation.Horizontal, | |
| BackgroundColor = Color.Black, | |
| Children = | |
| { | |
| boxView, | |
| new StackLayout | |
| { | |
| VerticalOptions = LayoutOptions.Center, | |
| Spacing = 0, | |
| Children = | |
| { | |
| nameLabel, | |
| birthdayLabel | |
| } | |
| } | |
| } | |
| }; | |
| nameLabel.SetBinding(Label.TextProperty, "Name"); | |
| birthdayLabel.SetBinding(Label.TextProperty, new Binding("Birthday", BindingMode.OneWay, null, null, "Born {0:d}")); | |
| boxView.SetBinding(BoxView.ColorProperty, "FavoriteColor"); | |
| stack.SetBinding(BackgroundColorProperty, "BackgroundColor"); | |
| return new ViewCell | |
| { | |
| View = stack | |
| }; | |
| }) | |
| }; | |
| Padding = new Thickness(10, Device.OnPlatform(20, 0, 0), 10, 5); | |
| Content = new StackLayout | |
| { | |
| Orientation = StackOrientation.Vertical, | |
| Children = | |
| { | |
| buttonAdd, | |
| buttonRemove, | |
| listView, | |
| } | |
| }; | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment