summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Tizen/Renderers/TableViewRenderer.cs
blob: 58b17a4b6e352b112ba5e5883f00c5c15b7b3285 (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
´╗┐using System;
using ElmSharp;

namespace Xamarin.Forms.Platform.Tizen
{
	public class TableViewRenderer : ViewRenderer<TableView, Native.TableView>, IVisualElementRenderer
	{
		internal static BindableProperty PresentationProperty = BindableProperty.Create("Presentation", typeof(View), typeof(TableSectionBase), null, BindingMode.OneWay, null, null, null, null, null as BindableProperty.CreateDefaultValueDelegate);

		public TableViewRenderer()
		{
			RegisterPropertyHandler(TableView.HasUnevenRowsProperty, UpdateHasUnevenRows);
			RegisterPropertyHandler(TableView.RowHeightProperty, UpdateRowHeight);
		}

		protected override void OnElementChanged(ElementChangedEventArgs<TableView> e)
		{
			if (Control == null)
			{
				SetNativeControl(new Native.TableView(Forms.Context.MainWindow));
				Control.ItemSelected += OnSelected;
			}

			if (e.OldElement != null)
			{
				e.OldElement.ModelChanged -= OnRootPropertyChanged;
			}

			if (e.NewElement != null)
			{
				e.NewElement.ModelChanged += OnRootPropertyChanged;
				Control.ApplyTableRoot(e.NewElement.Root);
			}

			base.OnElementChanged(e);
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				if (Element != null)
				{
					Element.ModelChanged -= OnRootPropertyChanged;
				}

				if (Control != null)
				{
					Control.ItemSelected -= OnSelected;
				}
			}

			base.Dispose(disposing);
		}

		void OnSelected(object sender, GenListItemEventArgs e)
		{
			var item = e.Item as GenListItem;

			if (item != null)
			{
				var clickedCell = item.Data as Native.ListView.ItemContext;
				if (null != clickedCell)
				{
					Element.Model.RowSelected(clickedCell.Cell);
				}
			}
		}

		void OnRootPropertyChanged(object sender, EventArgs e)
		{
			if (Element != null)
			{
				Control.ApplyTableRoot(Element.Root);
			}
		}

		void UpdateHasUnevenRows()
		{
			Control.SetHasUnevenRows(Element.HasUnevenRows);
		}

		void UpdateRowHeight()
		{
			Control.UpdateRealizedItems();
		}

	}
}