summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla44525.cs
blob: e159aa7117e9764628f3ebacc86266f333be92c2 (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
using System;
using System.Linq;
using System.Collections.Generic;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;
using System.ComponentModel;

namespace Xamarin.Forms.Controls
{
	[Preserve(AllMembers = true)]
	[Issue(IssueTracker.Bugzilla, 44525, "Listview Row Height Does Not Adapt In iOS")]
	public class Bugzilla44525 : TestContentPage
	{
		List<Person> _DataSource;

		[Preserve(AllMembers = true)]
		class CustomCell : ViewCell
		{
			public CustomCell()
			{
				Label age = new Label();
				Label name = new Label();
				StackLayout cellWrapper = new StackLayout();

				age.SetBinding(Label.TextProperty, "Age");
				name.SetBinding(Label.TextProperty, "Name");

				age.PropertyChanged += UpdateCell;
				name.PropertyChanged += UpdateCell;

				cellWrapper.Children.Add(age);
				cellWrapper.Children.Add(name);

				View = cellWrapper;
			}

			void UpdateCell(object sender, PropertyChangedEventArgs e)
			{
				if (e.PropertyName == Label.TextProperty.PropertyName)
				{
					ForceUpdateSize();
				}
			}
		}

		class Person : ViewModelBase
		{
			private string _Name;
			public string Name
			{
				get
				{
					return _Name;
				}
				set
				{
					if (_Name == value)
						return;

					_Name = value;
					OnPropertyChanged();
				}
			}

			private string _Age;
			public string Age
			{
				get
				{
					return _Age;
				}
				set
				{
					if (_Age == value)
						return;

					_Age = value;
					OnPropertyChanged();
				}
			}
		}

		protected override void Init()
		{
			_DataSource = Enumerable.Range(1, 100).Select(c => new Person { Name = $"Person {c}", Age = $"{c} year(s) old" }).ToList();

			var listView = new ListView(ListViewCachingStrategy.RecycleElement)
			{
				ItemTemplate = new DataTemplate(typeof(CustomCell)),
				ItemsSource = _DataSource,
				HasUnevenRows = true
			};

			var button = new Button { Text = "Click me" };
			button.Clicked += (sender, e) =>
			{
				var target = _DataSource[1];
				target.Name = "I am an exceptionally long string that should cause the label to wrap, thus increasing the size of the ViewCell such that the entirety of the string is readable by human eyes. Hurrah.";
			};

			Content = new StackLayout { Children = { button, listView } };
		}
	}
}