Skip to content

Commit a8888e4

Browse files
committed
complete switch toggle on content support
1 parent b541554 commit a8888e4

File tree

3 files changed

+50
-10
lines changed

3 files changed

+50
-10
lines changed

MainDemo.Wpf/Buttons.xaml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,12 +264,14 @@
264264
<ToggleButton Style="{StaticResource MaterialDesignSwitchAccentToggleButton}" VerticalAlignment="Center" Margin="8 0 0 0"
265265
ToolTip="MaterialDesignSwitchAccentToggleButton" IsChecked="True" />
266266
<ToggleButton Style="{StaticResource MaterialDesignSwitchToggleButton}" VerticalAlignment="Center" Margin="8 0 0 0"
267-
ToolTip="MaterialDesignSwitchToggleButton w/ Content" IsChecked="True">
268-
<ToggleButton.Content>
269-
<materialDesign:PackIcon Kind="Play"/>
270-
</ToggleButton.Content>
267+
ToolTip="MaterialDesignSwitchToggleButton with Content and ToggleButtonAssist.OnContent">
268+
<materialDesign:PackIcon Kind="Pin" RenderTransformOrigin=".5,.5">
269+
<materialDesign:PackIcon.RenderTransform>
270+
<RotateTransform Angle="45" />
271+
</materialDesign:PackIcon.RenderTransform>
272+
</materialDesign:PackIcon>
271273
<materialDesign:ToggleButtonAssist.OnContent>
272-
<materialDesign:PackIcon Kind="Headphones"/>
274+
<materialDesign:PackIcon Kind="Pin"/>
273275
</materialDesign:ToggleButtonAssist.OnContent>
274276
</ToggleButton>
275277
</StackPanel>

MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.ToggleButton.xaml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@
263263
<Setter Property="Foreground" Value="{DynamicResource PrimaryHueMidForegroundBrush}"/>
264264
<Setter Property="Background" Value="{DynamicResource PrimaryHueMidBrush}"/>
265265
<Setter Property="BorderThickness" Value="1"/>
266-
<Setter Property="Padding" Value="1"/>
266+
<Setter Property="Padding" Value="0 1 0 0"/>
267267
<Setter Property="Template">
268268
<Setter.Value>
269269
<ControlTemplate TargetType="{x:Type ToggleButton}">
@@ -357,8 +357,10 @@
357357
</Ellipse>
358358
</AdornerDecorator>
359359
<ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}"
360-
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
361-
FlowDirection="LeftToRight"/>
360+
Margin="{TemplateBinding Padding}"
361+
x:Name="ContentPresenter"
362+
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
363+
FlowDirection="LeftToRight"/>
362364
<Grid.RenderTransform>
363365
<TranslateTransform X="0" Y="0"/>
364366
</Grid.RenderTransform>
@@ -371,6 +373,14 @@
371373
<Setter TargetName="Track" Property="Fill" Value="{DynamicResource PrimaryHueLightBrush}" />
372374
<Setter Property="Foreground" Value="{DynamicResource PrimaryHueMidForegroundBrush}"/>
373375
</Trigger>
376+
<MultiTrigger>
377+
<MultiTrigger.Conditions>
378+
<Condition Property="IsChecked" Value="True" />
379+
<Condition Property="wpf:ToggleButtonAssist.HasOnContent" Value="True" />
380+
</MultiTrigger.Conditions>
381+
<Setter TargetName="ContentPresenter" Property="Content" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(wpf:ToggleButtonAssist.OnContent)}" />
382+
<Setter TargetName="ContentPresenter" Property="ContentTemplate" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(wpf:ToggleButtonAssist.OnContentTemplate)}" />
383+
</MultiTrigger>
374384
<Trigger Property="IsChecked" Value="False">
375385
<Setter Property="Foreground" Value="{DynamicResource PrimaryHueMidBrush}"/> <!-- Need to set Foreground to Background here instead of hardcoded PrimaryHueMidBrush -->
376386
</Trigger>

MaterialDesignThemes.Wpf/ToggleButtonAssist.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,43 @@
1-
using System.Windows;
1+
using System;
2+
using System.Windows;
23
using System.Windows.Controls.Primitives;
34

45
namespace MaterialDesignThemes.Wpf
56
{
67
public static class ToggleButtonAssist
78
{
9+
private static readonly DependencyPropertyKey HasOnContentPropertyKey =
10+
DependencyProperty.RegisterAttachedReadOnly(
11+
"HasOnContent", typeof(bool), typeof(ToggleButtonAssist),
12+
new PropertyMetadata(false));
13+
14+
public static readonly DependencyProperty HasOnContentProperty = HasOnContentPropertyKey.DependencyProperty;
15+
16+
private static void SetHasOnContent(DependencyObject element, object value)
17+
{
18+
element.SetValue(HasOnContentPropertyKey, value);
19+
}
20+
21+
/// <summary>
22+
/// Framework use only.
23+
/// </summary>
24+
/// <param name="element"></param>
25+
/// <returns></returns>
26+
public static bool GetHasOnContent(DependencyObject element)
27+
{
28+
return (bool)element.GetValue(HasOnContentProperty);
29+
}
30+
831
/// <summary>
932
/// Allows on (IsChecked) content to be provided on supporting <see cref="ToggleButton"/> styles.
1033
/// </summary>
1134
public static readonly DependencyProperty OnContentProperty = DependencyProperty.RegisterAttached(
12-
"OnContent", typeof (object), typeof (ToggleButtonAssist), new PropertyMetadata(default(object)));
35+
"OnContent", typeof (object), typeof (ToggleButtonAssist), new PropertyMetadata(default(object), OnContentPropertyChangedCallback));
36+
37+
private static void OnContentPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
38+
{
39+
SetHasOnContent(dependencyObject, dependencyPropertyChangedEventArgs.NewValue != null);
40+
}
1341

1442
/// <summary>
1543
/// Allows on (IsChecked) content to be provided on supporting <see cref="ToggleButton"/> styles.

0 commit comments

Comments
 (0)