summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.WP8/CellTemplateSelector.cs
blob: 09d939fdd31aae6e76874987cfd007b18fceb08c (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
´╗┐using System;
using System.Globalization;
using System.Windows;

namespace Xamarin.Forms.Platform.WinPhone
{
	public class HeightConverter : System.Windows.Data.IValueConverter
	{
		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			var val = (double)value;
			return val > 0 ? val : double.NaN;
		}

		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			throw new NotImplementedException();
		}
	}

	[Obsolete("Deprecated in favor of CellControl")]
	public class CellTemplateSelector : DataTemplateSelector
	{
		public static readonly DependencyProperty SourceProperty = DependencyProperty.Register("Source", typeof(object), typeof(CellTemplateSelector),
			new PropertyMetadata((o, e) => ((CellTemplateSelector)o).SetSource(e.OldValue, e.NewValue)));

		public CellTemplateSelector()
		{
			Loaded += (sender, args) => SetBinding(SourceProperty, new System.Windows.Data.Binding());
			Unloaded += (sender, args) =>
			{
				var cell = DataContext as Cell;
				if (cell != null)
					cell.SendDisappearing();
			};
		}

		public override System.Windows.DataTemplate SelectTemplate(object item, DependencyObject container)
		{
			var cell = item as Cell;
			if (cell == null)
				return null;

			var renderer = Registrar.Registered.GetHandler<ICellRenderer>(cell.GetType());
			return renderer.GetTemplate(cell);
		}

		void SetSource(object oldSource, object newSource)
		{
			var oldCell = oldSource as Cell;
			var newCell = newSource as Cell;

			if (oldCell != null)
				oldCell.SendDisappearing();

			if (newCell != null)
				newCell.SendAppearing();
		}
	}
}