Welcome to mirror list, hosted at ThFree Co, Russian Federation.

EditorPropertySelector.cs « Xamarin.PropertyEditing.Windows - github.com/xamarin/Xamarin.PropertyEditing.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e9b8209ef337aa888a5940341e17a71102d56827 (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
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Xamarin.PropertyEditing.Drawing;
using Xamarin.PropertyEditing.ViewModels;

namespace Xamarin.PropertyEditing.Windows
{
	internal class EditorTreeSelectorOptions
		: DependencyObject
	{
		public static readonly DependencyProperty ParentTemplateProperty = DependencyProperty.Register (
			nameof(ParentTemplate), typeof(DataTemplate), typeof(EditorTreeSelectorOptions), new PropertyMetadata (default(DataTemplate)));

		public DataTemplate ParentTemplate
		{
			get { return (DataTemplate) GetValue (ParentTemplateProperty); }
			set { SetValue (ParentTemplateProperty, value); }
		}

		public static readonly DependencyProperty EditorTemplateProperty = DependencyProperty.Register (
			nameof(EditorTemplate), typeof(DataTemplate), typeof(EditorTreeSelectorOptions), new PropertyMetadata (default(DataTemplate)));

		public DataTemplate EditorTemplate
		{
			get { return (DataTemplate) GetValue (EditorTemplateProperty); }
			set { SetValue (EditorTemplateProperty, value); }
		}
	}


	internal class EditorTreeSelector
		: DataTemplateSelector
	{
		public EditorTreeSelectorOptions Options
		{
			get;
			set;
		}

		public override DataTemplate SelectTemplate (object item, DependencyObject container)
		{
			var vm = item as PropertyViewModel;
			if (vm != null) {
				if (!vm.CanDelve)
					return Options.EditorTemplate;
				else
					return Options.ParentTemplate;
			}

			return Options.ParentTemplate;
		}
	}

	internal class EditorPropertySelector
		: DataTemplateSelector
	{
		public override DataTemplate SelectTemplate (object item, DependencyObject container)
		{
			if (item != null) {
				Type type = item.GetType ();
				DataTemplate template;
				if (!TryGetTemplate (type, out template)) {
					if (type.IsConstructedGenericType) {
						type = type.GetGenericTypeDefinition ();
						TryGetTemplate (type, out template);
					}
				}

				if (template != null)
					return template;
			}

			return base.SelectTemplate (item, container);
		}

		private readonly Dictionary<Type, DataTemplate> templates = new Dictionary<Type, DataTemplate> ();

		private bool TryGetTemplate (Type type, out DataTemplate template)
		{
			if (this.templates.TryGetValue (type, out template))
				return true;

			Type controlType;
			if (TypeMap.TryGetValue (type, out controlType)) {
				this.templates[type] = template = new DataTemplate (type) {
					VisualTree = new FrameworkElementFactory (controlType)
				};

				return true;
			}

			return false;
		}

		// We can improve on this, but it'll get us started
		private static readonly Dictionary<Type, Type> TypeMap = new Dictionary<Type, Type> {
			{ typeof(StringPropertyViewModel), typeof(StringEditorControl) },
			{ typeof(PropertyViewModel<bool>), typeof(BoolEditorControl) },
			{ typeof(BytePropertyViewModel), typeof(NumericEditorControl) },
			{ typeof(IntegerPropertyViewModel), typeof(NumericEditorControl) },
			{ typeof(FloatingPropertyViewModel), typeof(NumericEditorControl) },
			{ typeof(PointPropertyViewModel), typeof(PointEditorControl) },
			{ typeof(SizePropertyViewModel), typeof(SizeEditorControl) },
			{ typeof(PropertyViewModel<Thickness>), typeof(ThicknessEditorControl) },
			{ typeof(PropertyViewModel<CommonThickness>), typeof(ThicknessEditorControl) },
			{ typeof(PredefinedValuesViewModel<>), typeof(EnumEditorControl) },
			{ typeof(BrushPropertyViewModel), typeof(BrushEditorControl) },
		};
	}
}