summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.WP8/SwitchRenderer.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Xamarin.Forms.Platform.WP8/SwitchRenderer.cs')
-rw-r--r--Xamarin.Forms.Platform.WP8/SwitchRenderer.cs50
1 files changed, 50 insertions, 0 deletions
diff --git a/Xamarin.Forms.Platform.WP8/SwitchRenderer.cs b/Xamarin.Forms.Platform.WP8/SwitchRenderer.cs
new file mode 100644
index 00000000..e64fe227
--- /dev/null
+++ b/Xamarin.Forms.Platform.WP8/SwitchRenderer.cs
@@ -0,0 +1,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;
+ }
+ }
+} \ No newline at end of file