aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Touch/Controls/TouchToggleSlider.cs
blob: b1e7080fc08abb98485fc360cf8a5de24b0d517f (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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 String LeftText
        {
            get { return (String)GetValue(LeftTextProperty); }
            set { SetValue(LeftTextProperty, value); }
        }
        
        public static readonly DependencyProperty LeftTextProperty =
            DependencyProperty.Register("LeftText", typeof(String), typeof(TouchToggleSlider), new PropertyMetadata(""));

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

        public String RightText
        {
            get { return (String)GetValue(RightTextProperty); }
            set { SetValue(RightTextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for RightText.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty RightTextProperty =
            DependencyProperty.Register("RightText", typeof(String), typeof(TouchToggleSlider), new PropertyMetadata(""));



        public int ThumbWidth
        {
            get { return (int)GetValue(ThumbWidthProperty); }
            set { SetValue(ThumbWidthProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ThumbWidth.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ThumbWidthProperty =
            DependencyProperty.Register("ThumbWidth", typeof(int), typeof(TouchToggleSlider), new PropertyMetadata(0));




        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;
            SetThumbPosition();
        }

        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)
                {
                    if (ActualWidth != 0 &&  _grid_ellipse.ActualWidth == 0)
                    {
                        ani.To = new Thickness(ActualWidth - (ActualWidth/2) - 4, 0, 0, 0);
                    }
                    else
                    {
                        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);
            }
        }
    }
}