2016-03-22 23:02:25 +03:00
|
|
|
|
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();
|
|
|
|
|
|
2016-06-16 18:45:09 +03:00
|
|
|
|
IElementController ElementController => Element as IElementController;
|
|
|
|
|
|
2016-03-22 23:02:25 +03:00
|
|
|
|
protected override void OnElementChanged(ElementChangedEventArgs<Switch> e)
|
|
|
|
|
{
|
|
|
|
|
base.OnElementChanged(e);
|
|
|
|
|
|
|
|
|
|
var container = new Border { Child = _toggleSwitch };
|
|
|
|
|
_toggleSwitch.IsChecked = Element.IsToggled;
|
2016-06-16 18:45:09 +03:00
|
|
|
|
_toggleSwitch.Checked += (sender, args) => ElementController.SetValueFromRenderer(Switch.IsToggledProperty, true);
|
|
|
|
|
_toggleSwitch.Unchecked += (sender, args) => ElementController.SetValueFromRenderer(Switch.IsToggledProperty, false);
|
2016-03-22 23:02:25 +03:00
|
|
|
|
_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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|