summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.WinRT/NativeBindingExtensions.cs
blob: cc88c87e4b95311481caed7613f489fee844d708 (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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using Windows.UI.Xaml;
using static System.String;
#if WINDOWS_UWP

namespace Xamarin.Forms.Platform.UWP
#else

namespace Xamarin.Forms.Platform.WinRT
#endif
{
	public static class NativeBindingExtensions
	{
		public static void SetBinding(this FrameworkElement view, string propertyName, BindingBase bindingBase, string updateSourceEventName = null)
		{
			var binding = bindingBase as Binding;
			updateSourceEventName = updateSourceEventName ?? binding?.UpdateSourceEventName;

			if (IsNullOrEmpty(updateSourceEventName))
			{
				NativePropertyListener nativePropertyListener = null;
				if (bindingBase.Mode == BindingMode.TwoWay)
					nativePropertyListener = new NativePropertyListener(view, propertyName);

				NativeBindingHelpers.SetBinding(view, propertyName, bindingBase, nativePropertyListener as INotifyPropertyChanged);
				return;
			}

			NativeEventWrapper eventE = null;
			if (binding.Mode == BindingMode.TwoWay && !(view is INotifyPropertyChanged))
				eventE = new NativeEventWrapper(view, propertyName, updateSourceEventName);

			NativeBindingHelpers.SetBinding(view, propertyName, binding, eventE);
		}

		public static void SetBinding(this FrameworkElement view, BindableProperty targetProperty, BindingBase binding)
		{
			NativeBindingHelpers.SetBinding(view, targetProperty, binding);
		}

		public static void SetValue(this FrameworkElement target, BindableProperty targetProperty, object value)
		{
			NativeBindingHelpers.SetValue(target, targetProperty, value);
		}

		public static void SetBindingContext(this FrameworkElement target, object bindingContext, Func<FrameworkElement, IEnumerable<FrameworkElement>> getChildren = null)
		{
			NativeBindingHelpers.SetBindingContext(target, bindingContext, getChildren);
		}

		internal static void TransferbindablePropertiesToWrapper(this FrameworkElement target, View wrapper)
		{
			NativeBindingHelpers.TransferBindablePropertiesToWrapper(target, wrapper);
		}
	}
}