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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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);
            }
        }
    }
}