summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Pages/ListDataPage.cs
blob: 9b2c788a1f8111398dbb7eaf4bdfb24d431aff25 (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
´╗┐using Xamarin.Forms.Internals;

namespace Xamarin.Forms.Pages
{
	public class ListDataPageControl : ListView
	{
		public ListDataPageControl()
		{
			SetBinding(ItemTemplateProperty, new TemplateBinding(DataPage.DefaultItemTemplateProperty.PropertyName));
			SetBinding(SelectedItemProperty, new TemplateBinding(ListDataPage.SelectedItemProperty.PropertyName, BindingMode.TwoWay));
			SetBinding(ItemsSourceProperty, new TemplateBinding(DataPage.DataProperty.PropertyName));
		}
	}

	public class ListDataPage : DataPage
	{
		public static readonly BindableProperty DetailTemplateProperty = BindableProperty.Create(nameof(DetailTemplate), typeof(DataTemplate), typeof(ListDataPage), null);

		public static readonly BindableProperty SelectedItemProperty = BindableProperty.Create(nameof(SelectedItem), typeof(object), typeof(ListDataPage), null, BindingMode.TwoWay,
			propertyChanged: OnSelectedItemChanged);

		public DataTemplate DetailTemplate
		{
			get { return (DataTemplate)GetValue(DetailTemplateProperty); }
			set { SetValue(DetailTemplateProperty, value); }
		}

		public object SelectedItem
		{
			get { return GetValue(SelectedItemProperty); }
			set { SetValue(SelectedItemProperty, value); }
		}

		static async void OnSelectedItemChanged(BindableObject bindable, object oldValue, object newValue)
		{
			var self = (ListDataPage)bindable;
			DataTemplate template = self.DetailTemplate;
			if (newValue == null)
				return;

			Page detailPage;
			if (template == null)
			{
				detailPage = new DataPage();
			}
			else
			{
				detailPage = (Page)template.CreateContent(newValue, self);
			}

			detailPage.BindingContext = newValue;
			await self.Navigation.PushAsync(detailPage);

			self.SelectedItem = null;
		}
	}
}