Advertisement
PizzaCoder

ToggleButton.cs

Nov 13th, 2015
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.98 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Controls.Primitives;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Animation;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17.  
  18. namespace UI_Kit
  19. {
  20.     /// <summary>
  21.     /// Follow steps 1a or 1b and then 2 to use this custom control in a XAML file.
  22.     ///
  23.     /// Step 1a) Using this custom control in a XAML file that exists in the current project.
  24.     /// Add this XmlNamespace attribute to the root element of the markup file where it is
  25.     /// to be used:
  26.     ///
  27.     ///     xmlns:MyNamespace="clr-namespace:UI_Kit"
  28.     ///
  29.     ///
  30.     /// Step 1b) Using this custom control in a XAML file that exists in a different project.
  31.     /// Add this XmlNamespace attribute to the root element of the markup file where it is
  32.     /// to be used:
  33.     ///
  34.     ///     xmlns:MyNamespace="clr-namespace:UI_Kit;assembly=UI_Kit"
  35.     ///
  36.     /// You will also need to add a project reference from the project where the XAML file lives
  37.     /// to this project and Rebuild to avoid compilation errors:
  38.     ///
  39.     ///     Right click on the target project in the Solution Explorer and
  40.     ///     "Add Reference"->"Projects"->[Browse to and select this project]
  41.     ///
  42.     ///
  43.     /// Step 2)
  44.     /// Go ahead and use your control in the XAML file.
  45.     ///
  46.     ///     <MyNamespace:CodedToggle/>
  47.     ///
  48.     /// </summary>
  49.     public class CodedToggle : ToggleButton
  50.     {
  51.         public CodedToggle()
  52.         {
  53.             this.IsChecked = false;
  54.             this.Height = 47;
  55.             this.Width = 94;
  56.             // DefaultStyleKeyProperty.OverrideMetadata(typeof(CodedToggle), new FrameworkPropertyMetadata(typeof(CodedToggle)));
  57.             ControlTemplate template = new ControlTemplate();
  58.             FrameworkElementFactory grid = new FrameworkElementFactory(typeof(Grid)); grid.Name = "Container";
  59.             FrameworkElementFactory active = new FrameworkElementFactory(typeof(Border)); active.Name = "Active";
  60.             FrameworkElementFactory inactive = new FrameworkElementFactory(typeof(Border)); inactive.Name = "Inactive";
  61.             FrameworkElementFactory button = new FrameworkElementFactory(typeof(Ellipse)); button.Name="ToggleButton";
  62.  
  63.             inactive.SetBinding(Border.CornerRadiusProperty, new Binding("Height") { RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent), Converter = new HalfRadius() });
  64.             inactive.SetBinding(Border.WidthProperty, new Binding("Width") { RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent) });
  65.             inactive.SetBinding(Border.HeightProperty, new Binding("Height") { RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent), Converter = new LessHeight() });
  66.             inactive.SetValue(Border.BackgroundProperty, new SolidColorBrush(Color.FromArgb(255, 68, 68, 68)));
  67.             inactive.SetValue(Border.HorizontalAlignmentProperty, HorizontalAlignment.Left);
  68.  
  69.             active.SetBinding(Border.CornerRadiusProperty, new Binding("Height") { RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent), Converter = new HalfRadius() });
  70.             active.SetValue(Border.WidthProperty, this.Height);
  71.             active.SetBinding(Border.HeightProperty, new Binding("Height") { RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent), Converter = new LessHeight() });
  72.             active.SetBinding(Border.BackgroundProperty, new Binding("Background") { RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent) });
  73.             active.SetValue(Border.HorizontalAlignmentProperty, HorizontalAlignment.Left);
  74.  
  75.             button.SetBinding(Ellipse.WidthProperty, new Binding("Height") { RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent) });
  76.             button.SetBinding(Ellipse.HeightProperty, new Binding("Height") { RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent) });
  77.             button.SetValue(Ellipse.FillProperty, new SolidColorBrush(Colors.White));
  78.             button.SetValue(Ellipse.StrokeProperty, null);
  79.             button.SetValue(Ellipse.HorizontalAlignmentProperty, HorizontalAlignment.Left);
  80.             button.AddHandler(Ellipse.MouseDownEvent, new MouseButtonEventHandler(Toggle));
  81.  
  82.             grid.AppendChild(inactive);
  83.             grid.AppendChild(active);
  84.             grid.AppendChild(button);
  85.  
  86.             template.VisualTree = grid;
  87.             this.Template = template;
  88.         }
  89.         void Toggle(object sender, EventArgs e)
  90.         {
  91.             DependencyObject active = GetTemplateChild("Active");
  92.             DependencyObject button = GetTemplateChild("ToggleButton");
  93.             Storyboard sb = new Storyboard();
  94.  
  95.             ThicknessAnimation ta = new ThicknessAnimation();
  96.             Storyboard.SetTargetProperty(ta, new PropertyPath("Margin"));
  97.             Storyboard.SetTarget(ta, button);
  98.             ta.Duration = TimeSpan.FromSeconds(0.2);
  99.            
  100.  
  101.             DoubleAnimation da = new DoubleAnimation();
  102.             Storyboard.SetTargetProperty(da, new PropertyPath("Width"));
  103.             Storyboard.SetTarget(da, active);
  104.             da.Duration = TimeSpan.FromSeconds(0.2);
  105.            
  106.             if ((bool)IsChecked)
  107.             {
  108.                 ta.To = new Thickness(0);
  109.                 da.To = (double)button.GetValue(Ellipse.WidthProperty);
  110.             }
  111.             else
  112.             {
  113.                 ta.To = new Thickness(this.Width - (double)button.GetValue(Ellipse.WidthProperty), 0, 0, 0);
  114.                 da.To = this.Width;
  115.             }
  116.  
  117.             IsChecked = !IsChecked;
  118.             sb.Children.Add(ta);
  119.             sb.Children.Add(da);
  120.             sb.Begin();
  121.         }
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement