summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/ListViewViewCellBinding.cs
blob: f0b0025bf6794697121da0351b5023f3d6d0a99b (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using System;
using System.Collections.ObjectModel;
using System.Threading;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;

#if UITEST
using NUnit.Framework;
using Xamarin.UITest;
#endif

namespace Xamarin.Forms.Controls
{
	public class GenericValueConverter : IValueConverter
	{
		Func<object, object> _convert;
		Func<object, object> _back;
		public GenericValueConverter(Func<object, object> convert, Func<object, object> back = null)
		{
			_convert = convert;
			_back = back;
		}

		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			return _convert(value);
		}

		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			return _back(value);
		}
	}
}

namespace Xamarin.Forms.Controls.Issues
{
	[Preserve (AllMembers = true)]
	public class Expense
	{
		public string Name { get; private set; }
		public decimal Amount { get; private set; }

		public Expense (string name, decimal amount)
		{
			Name = name;
			Amount = amount;
		}
	}

	[Preserve (AllMembers = true)]
	public class ExpenseListViewCell : ViewCell
	{
		public ExpenseListViewCell ()
		{
			var expenseNameLabel = new Label ();
			expenseNameLabel.SetBinding (Label.TextProperty, "Name");

			var expenseAmountLabel = new Label ();
			var expenseAmountToStringConverter = new GenericValueConverter (obj => string.Format ("{0:C}", ((decimal)obj)));
			expenseAmountLabel.SetBinding (Label.TextProperty, new Binding ("Amount", converter: expenseAmountToStringConverter));

			var layout = new StackLayout ();

			layout.Children.Add (expenseNameLabel);
			layout.Children.Add (expenseAmountLabel);

			View = layout;
		}

		protected override void OnBindingContextChanged ()
		{
			// Fixme : this is happening because the View.Parent is getting 
			// set after the Cell gets the binding context set on it. Then it is inheriting
			// the parents binding context.
			View.BindingContext = BindingContext;
			base.OnBindingContextChanged ();
		}
	}

	[Preserve (AllMembers = true)]
	[Issue (IssueTracker.None, 0, "ListView ViewCell binding", PlatformAffected.All)]
	public class ListViewViewCellBinding : TestContentPage
	{

		// Binding issue with view cells
		public ObservableCollection<Expense> Expenses;

		protected override void Init ()
		{
			//BindingContext = this;

			Expenses = new ObservableCollection<Expense> {
				new Expense ("1", 100.0m),
				new Expense ("2", 200.0m),
				new Expense ("3", 300.0m)
			};

			var listView = new ListView ();

			listView.ItemsSource = Expenses;
			listView.ItemTemplate = new DataTemplate (typeof (ExpenseListViewCell));

			var layout = new StackLayout ();
			int numberAdded = 3;

			var label = new Label {
				Text = numberAdded.ToString()
			};

			var removeBtn = new Button { Text = "Remove" };
			removeBtn.Clicked += (s, e) => {
				if (numberAdded > 0) {
					numberAdded--;
					Expenses.RemoveAt (0);
					label.Text = numberAdded.ToString ();
				}
			};
			var addBtn = new Button { Text = "Add" };
			addBtn.Clicked += (s, e) => {
				Expenses.Add (new Expense ("4", 400.0m));
				numberAdded++;
				label.Text = numberAdded.ToString ();
			};

			
			layout.Children.Add (label);
			layout.Children.Add (removeBtn);
			layout.Children.Add (addBtn);
			layout.Children.Add (listView);

			Content = layout;
		}

#if UITEST
		[Test]
		public void ListViewViewCellBindingTestsAllElementsPresent ()
		{
			RunningApp.WaitForElement (q => q.Marked ("Remove"));
			RunningApp.WaitForElement (q => q.Marked ("Add"));
			RunningApp.WaitForElement (q => q.Marked ("1"));
			RunningApp.WaitForElement (q => q.Marked ("$100.00"));
			RunningApp.WaitForElement (q => q.Marked ("2"));
			RunningApp.WaitForElement (q => q.Marked ("$200.00"));
			RunningApp.WaitForElement (q => q.Marked ("3"));
			RunningApp.WaitForElement (q => q.Marked ("$300.00"));

			RunningApp.Screenshot ("All elements exist");
		}

		[Test]
		public void ListViewViewCellBindingTestsAddListItem () 
		{
			RunningApp.Tap (q => q.Button ("Add"));
			RunningApp.WaitForElement (q => q.Marked ("4"));
			RunningApp.WaitForElement (q => q.Marked ("$400.00"));
			RunningApp.Screenshot ("List item added");
		}

		[Test]
		public void ListViewViewCellBindingTestsRemoveListItem () 
		{
			RunningApp.Tap (q => q.Button ("Remove"));
			RunningApp.WaitForNoElement (q => q.Marked ("1"));
			RunningApp.WaitForNoElement (q => q.Marked ("$100.00"));
			RunningApp.Screenshot ("List item removed");
		}
#endif

	}
}