aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Touch/Controls/TouchToggleSlider.cs
blob: 79afdfaabc4077cb0fd7d5f980945583e0b25298 (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
4
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Tango.Touch.Controls
{
    public class TouchToggleSlider : ToggleButton
    {
        private Grid _grid_ellipse;

        static TouchToggleSlider()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(TouchToggleSlider), new FrameworkPropertyMetadata(typeof(TouchToggleSlider)));
        }

        public CornerRadius CornerRadius
        {
            get { return (CornerRadius)GetValue(CornerRadiusProperty); }
            set { SetValue(CornerRadiusProperty, value); }
        }
        public static readonly DependencyProperty CornerRadiusProperty =
            DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(TouchToggleSlider), new PropertyMetadata(default(CornerRadius)));

        public Brush ThumbBrush
        {
            get { return (Brush)GetValue(ThumbBrushProperty); }
            set { SetValue(ThumbBrushProperty, value); }
        }
        public static readonly DependencyProperty ThumbBrushProperty =
            DependencyProperty.Register("ThumbBrush", typeof(Brush), typeof(TouchToggleSlider), new PropertyMetadata(null));

        public Object UncheckedContent
        {
            get { return (Object)GetValue(UncheckedContentProperty); }
            set { SetValue(UncheckedContentProperty, value); }
        }
        public static readonly DependencyProperty UncheckedContentProperty =
            DependencyProperty.Register("UncheckedContent", typeof(Object), typeof(TouchToggleSlider), new PropertyMetadata(null));

        public Object CheckedContent
        {
            get { return (Object)GetValue(CheckedContentProperty); }
            set { SetValue(CheckedContentProperty, value); }
        }
        public static readonly DependencyProperty CheckedContentProperty =
            DependencyProperty.Register("CheckedContent", typeof(Object), typeof(TouchToggleSlider), new PropertyMetadata(null));

        public Object UncheckedThumbContent
        {
            get { return (Object)GetValue(UncheckedThumbContentProperty); }
            set { SetValue(UncheckedThumbContentProperty, value); }
        }
        public static readonly DependencyProperty UncheckedThumbContentProperty =
            DependencyProperty.Register("UncheckedThumbContent", typeof(Object), typeof(TouchToggleSlider), new PropertyMetadata(null));

        public Object CheckedThumbContent
        {
            get { return (Object)GetValue(CheckedThumbContentProperty); }
            set { SetValue(CheckedThumbContentProperty, value); }
        }
        public static readonly DependencyProperty CheckedThumbContentProperty =
            DependencyProperty.Register("CheckedThumbContent", typeof(Object), typeof(TouchToggleSlider), new PropertyMetadata(null));

        public Brush UncheckedBackground
        {
            get { return (Brush)GetValue(UncheckedBackgroundProperty); }
            set { SetValue(UncheckedBackgroundProperty, value); }
        }
        public static readonly DependencyProperty UncheckedBackgroundProperty =
            DependencyProperty.Register("UncheckedBackground", typeof(Brush), typeof(TouchToggleSlider), new PropertyMetadata(null));

        public Brush CheckedBackground
        {
            get { return (Brush)GetValue(CheckedBackgroundProperty); }
            set { SetValue(CheckedBackgroundProperty, value); }
        }
        public static readonly DependencyProperty CheckedBackgroundProperty =
            DependencyProperty.Register("CheckedBackground", typeof(Brush), typeof(TouchToggleSlider), new PropertyMetadata(null));

        public DataTemplate UncheckedThumbContentTemplate
        {
            get { return (DataTemplate)GetValue(UncheckedThumbContentTemplateProperty); }
            set { SetValue(UncheckedThumbContentTemplateProperty, value); }
        }
        public static readonly DependencyProperty UncheckedThumbContentTemplateProperty =
            DependencyProperty.Register("UncheckedThumbContentTemplate", typeof(DataTemplate), typeof(TouchToggleSlider), new PropertyMetadata(null));

        public DataTemplate CheckedThumbContentTemplate
        {
            get { return (DataTemplate)GetValue(CheckedThumbContentTemplateProperty); }
            set { SetValue(CheckedThumbContentTemplateProperty, value); }
        }
        public static readonly DependencyProperty CheckedThumbContentTemplateProperty =
            DependencyProperty.Register("CheckedThumbContentTemplate", typeof(DataTemplate), typeof(TouchToggleSlider), new PropertyMetadata(null));

        public TouchToggleSlider()
        {
            Loaded += TouchToggleSlider_Loaded;
        }

        private void TouchToggleSlider_Loaded(object sender, RoutedEventArgs e)
        {
            SetThumbPosition();
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            _grid_ellipse = GetTemplateChild("PART_GridEllipse") as Grid;
        }

        protected override void OnChecked(RoutedEventArgs e)
        {
            base.OnChecked(e);
            SetThumbPosition();
        }

        protected override void OnUnchecked(RoutedEventArgs e)
        {
            base.OnUnchecked(e);
            SetThumbPosition();
        }

        private void SetThumbPosition()
        {
            if (_grid_ellipse != null)
            {
                ThicknessAnimation ani = new ThicknessAnimation();
                ani.Duration = TimeSpan.FromSeconds(0.2);

                if (IsChecked.Value)
                {
                    ani.To = new Thickness(ActualWidth - _grid_ellipse.ActualWidth - 4, 0, 0, 0);
                }
                else
                {
                    ani.To = new Thickness(0, 0, 0, 0);
                }

                _grid_ellipse.BeginAnimation(Grid.MarginProperty, ani);
            }
        }
    }
}