XAML Code:
The code is as follows |
Copy Code |
<usercontrol x:class= "Kaitone.DetectiveHelper.UI.Controls.ToggleSwitchButton" Xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml" Xmlns:mc= "http://schemas.openxmlformats.org/markup-compatibility/2006" Xmlns:d= "http://schemas.microsoft.com/expression/blend/2008" Mc:ignorable= "D" d:designheight= "D:designwidth=" > <grid x:name= "LayoutRoot" background= "Transparent" Width= "height=" > <border borderbrush= "#cccccc" borderthickness= "1" margin= "4,2" padding= "0" cornerradius= "8" background= "#2ecc71" > <rectangle name= "FillRectangle" Fill= "#e74c3c" radiusx= "8" radiusy= "8" visibility= "collapsed"/> </Border> <border name= "Slideborder" Borderbrush= "#aaaaaa" borderthickness= "1" Horizontalalignment= "left" cornerradius= "width=" "height=" > <rectangle stroke= "Brown" Fill= "#FFF1F1F1" strokethickness= "0" Width= "radiusx=" radiusy= "" "/> </Border> </Grid> </UserControl> |
CS code: Primarily refactoring a http://www.111cn.net of events and return parameters:
The code is as follows |
Copy Code |
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.Data; Using System.Windows.Documents; Using System.Windows.Input; Using System.Windows.Media; Using System.Windows.Media.Imaging; Using System.Windows.Navigation; Using System.Windows.Shapes; Namespace Kaitone.DetectiveHelper.UI.Controls { <summary> The interactive logic of Toggleswitchbutton.xaml </summary> public partial class Toggleswitchbutton:usercontrol { public static readonly DependencyProperty Ischeckedproperty = Dependencyproperty.register ("ischecked", typeof (BOOL), typeof (Toggleswitchbutton), new PropertyMetadata (Default ( BOOL), onischeckedchanged); public event Routedeventhandler Checked; public event Routedeventhandler UnChecked; public bool IsChecked { get {return (bool) GetValue (Ischeckedproperty);} set {SetValue (Ischeckedproperty, value);} } Public Toggleswitchbutton () { InitializeComponent (); } private static void Onischeckedchanged (DependencyObject obj, DependencyPropertyChangedEventArgs args) { (obj as Toggleswitchbutton). Onischeckedchanged (args); } private void Onischeckedchanged (DependencyPropertyChangedEventArgs args) { Fillrectangle.visibility = ischecked? Visibility.Visible:Visibility.Collapsed; Slideborder.horizontalalignment = ischecked? HorizontalAlignment.Right:HorizontalAlignment.Left; if (ischecked && Checked!= null) { Checked (This, new RoutedEventArgs ()); } if (! ischecked && UnChecked!= null) { UnChecked (This, new RoutedEventArgs ()); } } protected override void Onmouseleftbuttonup (MouseButtonEventArgs args) { Args. Handled = true; IsChecked ^= true; Base. Onmouseleftbuttonup (args); } } } |