WPF Custom checkbox switch Control style example

Source: Internet
Author: User
Tags visibility xmlns

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);
}
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.