summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls/ControlGalleryPages/ListViewSelectionColor.cs
blob: e1ce2681089d5710092c0b0b9596ede408fd4039 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms.Internals;

namespace Xamarin.Forms.Controls
{
	internal sealed class ListViewSelectionColor : ContentPage
	{
		[Preserve (AllMembers = true)]
		internal sealed class GroupHeaderTemplate : ViewCell
		{
			public GroupHeaderTemplate ()
			{
				var label = new Label { BackgroundColor = Color.Red };
				label.SetBinding (Label.TextProperty, "Key");
				View = label;
			}
		}

		[Preserve (AllMembers = true)]
		internal sealed class GroupItemTemplate : ViewCell
		{
			public GroupItemTemplate ()
			{
				var label = new Label { BackgroundColor = Color.Green };
				label.SetBinding (Label.TextProperty, "Name");
				View = label;
			}
		}

		[Preserve (AllMembers = true)]
		internal sealed class ItemTemplate : ViewCell
		{
			public ItemTemplate ()
			{
				var label = new Label { BackgroundColor = Color.Green };
				label.SetBinding (Label.TextProperty, "Name");
				View = label;
			}
		}

		[Preserve (AllMembers = true)]
		internal sealed class Artist
		{
			public string Name { get; private set; }

			public Artist (string name)
			{
				Name = name;
			}
		}

		[Preserve (AllMembers = true)]
		internal sealed class Grouping<K, T> : ObservableCollection<T>
		{
			public K Key { get; private set; }

			public Grouping (K key, IEnumerable<T> values)
			{
				Key = key;

				foreach (T value in values) {
					Items.Add (value);
				}
			}
		}

		Button _swapListButton;

		[Preserve (AllMembers = true)]
		public ListViewSelectionColor()
		{
			Title = "ListView ScrollTo";

			var itemSource = new [] {
				new { Name = "John Hassel" },
				new { Name = "Brian Eno" },
				new { Name = "Rober Fripp" },
				new { Name = "Edgar Froese" }
			};

			var groupedItemSource = new List<Grouping<string, Artist>> {
				new Grouping<string, Artist> ("Prog", new List<Artist> { new Artist ("King Crimson"), new Artist ("Yes"), new Artist ("Rush") }),
				new Grouping<string, Artist> ("Techno", new List<Artist> { new Artist ("Juan Atkins"), new Artist ("Jeff Mills"), new Artist ("Gerald Donald") } ),
				new Grouping<string, Artist> ("Pop", new List<Artist> { new Artist ("Japan"), new Artist ("Roxy Music"), new Artist ("Talking Heads") }),
			};

			var normalList = new ListView {
				ItemTemplate = new DataTemplate (typeof (ItemTemplate)),
				ItemsSource = itemSource
			};
			var groupedList = new ListView {
				IsGroupingEnabled = true,
				GroupHeaderTemplate = new DataTemplate (typeof (GroupHeaderTemplate)),
				ItemTemplate = new DataTemplate (typeof (GroupItemTemplate)),
				ItemsSource = groupedItemSource
			};

			var currentList = normalList;
			_swapListButton = new Button {
				Text = "Swap List Type",
				Command = new Command (() => {
					if (currentList == normalList) {
						SetContent (groupedList);
						currentList = groupedList;
					} else {
						SetContent (normalList);
						currentList = normalList;
					}
				})
			};

			var clear = new Button {
				Text = "Clear",
				Command = new Command (() => {
					currentList.SelectedItem = null;
				})
			};

			Content = new StackLayout {
				Children = {
					new StackLayout {
						Orientation = StackOrientation.Horizontal,
						Children = {
							_swapListButton,
							clear
						}
					},
					normalList
				}
			};

		}

		void SetContent (ListView list)
		{
			Content = new StackLayout {
				Children = {
					_swapListButton,
					list
				}
			};
		}

		
	}
}