Skip to content

Instantly share code, notes, and snippets.

@xgenvn
Created September 6, 2017 04:33
Show Gist options
  • Select an option

  • Save xgenvn/7cbd29f223f291661a2a29696fd1e6ef to your computer and use it in GitHub Desktop.

Select an option

Save xgenvn/7cbd29f223f291661a2a29696fd1e6ef to your computer and use it in GitHub Desktop.
ToggleSwitch WPF Customization
<Style TargetType="{x:Type CheckBox}">
<Setter Property="Background" Value="{DynamicResource WhiteBrush}"/>
<Setter Property="BorderBrush" Value="{DynamicResource CheckBoxBrush}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="controls:ControlsHelper.FocusBorderBrush" Value="{DynamicResource HighlightBrush}"/>
<Setter Property="controls:ControlsHelper.MouseOverBorderBrush" Value="{DynamicResource CheckBoxMouseOverBrush}"/>
<Setter Property="FontFamily" Value="{DynamicResource ContentFontFamily}"/>
<Setter Property="FontSize" Value="{DynamicResource ContentFontSize}"/>
<Setter Property="Foreground" Value="{DynamicResource LabelTextBrush}"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="Padding" Value="6,0,0,0"/>
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type CheckBox}">
<Grid x:Name="root">
<Grid.ColumnDefinitions>
<ColumnDefinition x:Name="LeftCol" Width="32"/>
<ColumnDefinition x:Name="RightCol" Width="*"/>
</Grid.ColumnDefinitions>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="Disabled">
<Storyboard>
<DoubleAnimation Duration="0" To="0.55" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="contentPresenter"/>
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="disabled"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="CheckStates">
<VisualState x:Name="Checked">
<Storyboard>
<ThicknessAnimation Duration="0" To="0" Storyboard.TargetProperty="BorderThickness" Storyboard.TargetName="border"/>
<ColorAnimation Duration="0" To="#3E649F" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" Storyboard.TargetName="border" />
<DoubleAnimation Duration="0" To="22" Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.X)" Storyboard.TargetName="checkBox"/>
<ColorAnimation Duration="0" To="White" Storyboard.TargetProperty="(Ellipse.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="checkBox" />
</Storyboard>
</VisualState>
<VisualState x:Name="Unchecked"/>
<VisualState x:Name="Indeterminate">
<Storyboard>
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="IndeterminateCheck"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid x:Name="PART_CHECKBOX" >
<Border x:Name="border" BorderThickness="2" Margin="-6,0" CornerRadius="10" Height="20" BorderBrush="Black" Background="Transparent" />
<Border x:Name="normal" BorderBrush="Transparent" BorderThickness="0" Background="Transparent" Height="18" Width="18"/>
<Ellipse x:Name="checkBox" Fill="Black" HorizontalAlignment="Left" Stretch="Fill" UseLayoutRounding="False" Width="10" Height="10">
<Ellipse.RenderTransform>
<TranslateTransform />
</Ellipse.RenderTransform>
</Ellipse>
<Rectangle x:Name="IndeterminateCheck" Fill="{DynamicResource GrayBrush3}" Height="3" Opacity="0" Width="8"/>
<Border x:Name="disabled" BorderThickness="{TemplateBinding BorderThickness}" Background="{DynamicResource SemiTransparentWhiteBrush}" Height="18" Opacity="0" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Width="18"/>
</Grid>
<ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Grid.Column="1" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
</Trigger>
<Trigger Property="IsPressed" Value="True">
</Trigger>
<Trigger Property="IsFocused" Value="True">
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Opacity" TargetName="root" Value="0.5"/>
</Trigger>
<Trigger Property="FlowDirection" Value="RightToLeft">
<Setter Property="LayoutTransform" TargetName="checkBox">
<Setter.Value>
<ScaleTransform ScaleX="-1"/>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="controls:ToggleButtonHelper.ContentDirection" Value="RightToLeft">
<Setter Property="Padding" Value="0,0,6,0"/>
<Setter Property="Width" TargetName="LeftCol" Value="*"/>
<Setter Property="Grid.Column" TargetName="PART_CHECKBOX" Value="1"/>
<Setter Property="Width" TargetName="RightCol" Value="18"/>
<Setter Property="Grid.Column" TargetName="contentPresenter" Value="0"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="VerticalContentAlignment" Value="Center"/>
</Style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment