summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.WP8/SwitchRenderer.cs
blob: e64fe227c7813368fa1bedfd0a73d566b1589734 (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
´╗┐using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Phone.Controls.Primitives;

namespace Xamarin.Forms.Platform.WinPhone
{
	public class SwitchRenderer : ViewRenderer<Switch, Border>
	{
		readonly ToggleSwitchButton _toggleSwitch = new ToggleSwitchButton();

		protected override void OnElementChanged(ElementChangedEventArgs<Switch> e)
		{
			base.OnElementChanged(e);

			var container = new Border { Child = _toggleSwitch };
			_toggleSwitch.IsChecked = Element.IsToggled;
			_toggleSwitch.Checked += (sender, args) => ((IElementController)Element).SetValueFromRenderer(Switch.IsToggledProperty, true);
			_toggleSwitch.Unchecked += (sender, args) => ((IElementController)Element).SetValueFromRenderer(Switch.IsToggledProperty, false);
			_toggleSwitch.HorizontalAlignment = HorizontalAlignment.Right;

			SetNativeControl(container);
		}

		protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			base.OnElementPropertyChanged(sender, e);

			if (e.PropertyName == Switch.IsToggledProperty.PropertyName)
			{
				if (_toggleSwitch.IsChecked != Element.IsToggled)
					_toggleSwitch.IsChecked = Element.IsToggled;
			}
			else if (e.PropertyName == VisualElement.IsEnabledProperty.PropertyName)
				UpdateSwitchIsEnabled();
		}

		protected override void UpdateNativeWidget()
		{
			base.UpdateNativeWidget();

			UpdateSwitchIsEnabled();
		}

		void UpdateSwitchIsEnabled()
		{
			_toggleSwitch.IsEnabled = Element.IsEnabled;
		}
	}
}