Skip to content

Commit 1a2d9e0

Browse files
laxmyKeboo
authored andcommitted
#1408: Adding Track Background to SwitchToggleButton (#1460)
* #1408: Adding Track Background to SwitchToggleButton * Updating to have two brushes for the background Updated demo to reflect change. Fixing binding error on the off background color.
1 parent 2733b02 commit 1a2d9e0

File tree

4 files changed

+80
-30
lines changed

4 files changed

+80
-30
lines changed

MainDemo.Wpf/Toggles.xaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,16 @@
149149
</materialDesign:ToggleButtonAssist.OnContent>
150150
</ToggleButton>
151151
</smtx:XamlDisplay>
152+
<smtx:XamlDisplay Key="buttons_64" VerticalAlignment="Center" Margin="8 0 0 0">
153+
<ToggleButton Style="{StaticResource MaterialDesignSwitchToggleButton}" ToolTip="MaterialDesignSwitchToggleButton with Custom Track Background">
154+
<materialDesign:ToggleButtonAssist.SwitchTrackOnBackground>
155+
<SolidColorBrush Color="Green"/>
156+
</materialDesign:ToggleButtonAssist.SwitchTrackOnBackground>
157+
<materialDesign:ToggleButtonAssist.SwitchTrackOffBackground>
158+
<SolidColorBrush Color="Red"/>
159+
</materialDesign:ToggleButtonAssist.SwitchTrackOffBackground>
160+
</ToggleButton>
161+
</smtx:XamlDisplay>
152162
</StackPanel>
153163
<StackPanel Grid.Column="0" Grid.Row="4" Margin="0 24 0 0" Orientation="Horizontal">
154164
<smtx:XamlDisplay Key="buttons_53" Margin="5 0 0 0" VerticalAlignment="Center">

MaterialDesignThemes.Wpf/Converters/BrushToRadialGradientBrushConverter.cs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,25 @@
66

77
namespace MaterialDesignThemes.Wpf.Converters
88
{
9-
public class BrushToRadialGradientBrushConverter : IValueConverter
10-
{
11-
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
12-
{
13-
var solidColorBrush = value as SolidColorBrush;
14-
if (solidColorBrush == null) return Binding.DoNothing;
9+
public class BrushToRadialGradientBrushConverter : IValueConverter
10+
{
11+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
12+
{
13+
var solidColorBrush = value as SolidColorBrush;
14+
if (solidColorBrush == null) return Binding.DoNothing;
1515

16-
return new RadialGradientBrush(solidColorBrush.Color, Colors.Transparent)
17-
{
18-
Center = new Point(.5, .5),
19-
GradientOrigin = new Point(.5, .5),
20-
RadiusX = .75,
21-
RadiusY = .75,
22-
Opacity = .39
23-
};
24-
}
16+
return new RadialGradientBrush(solidColorBrush.Color, Colors.Transparent) {
17+
Center = new Point(.5, .5),
18+
GradientOrigin = new Point(.5, .5),
19+
RadiusX = .75,
20+
RadiusY = .75,
21+
Opacity = .39
22+
};
23+
}
2524

26-
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
27-
{
28-
return Binding.DoNothing;
29-
}
30-
}
25+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
26+
{
27+
return Binding.DoNothing;
28+
}
29+
}
3130
}

MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.ToggleButton.xaml

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
1+
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
22
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
33
xmlns:wpf="clr-namespace:MaterialDesignThemes.Wpf"
44
xmlns:system="clr-namespace:System;assembly=mscorlib"
@@ -263,6 +263,8 @@
263263
<Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
264264
<Setter Property="Foreground" Value="{DynamicResource PrimaryHueMidForegroundBrush}"/>
265265
<Setter Property="Background" Value="{DynamicResource PrimaryHueMidBrush}"/>
266+
<Setter Property="wpf:ToggleButtonAssist.SwitchTrackOnBackground" Value="{DynamicResource PrimaryHueLightBrush}"/>
267+
<Setter Property="wpf:ToggleButtonAssist.SwitchTrackOffBackground" Value="Black"/>
266268
<Setter Property="BorderThickness" Value="1"/>
267269
<Setter Property="Padding" Value="0 1 0 0"/>
268270
<Setter Property="Template">
@@ -331,9 +333,17 @@
331333
</VisualStateGroup>
332334
</VisualStateManager.VisualStateGroups>
333335
<Grid>
334-
<Rectangle x:Name="Track"
335-
Fill="Black" HorizontalAlignment="Left" Height="15" Margin="4.211,5,4.211,0" Stroke="{x:Null}" VerticalAlignment="Top" Width="40" RadiusY="7.5" RadiusX="7.5" Opacity="0.26"/>
336-
336+
<Rectangle x:Name="Track"
337+
Fill= "{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(wpf:ToggleButtonAssist.SwitchTrackOffBackground)}"
338+
Opacity="0.26"
339+
HorizontalAlignment="Left"
340+
Height="15"
341+
Margin="4.211,5,4.211,0"
342+
Stroke="{x:Null}"
343+
VerticalAlignment="Top"
344+
Width="40"
345+
RadiusY="7.5"
346+
RadiusX="7.5"/>
337347
<Grid x:Name="ThumbHolder"
338348
HorizontalAlignment="Left" VerticalAlignment="Top">
339349
<Ellipse x:Name="RippleThumb"
@@ -372,7 +382,7 @@
372382
<ControlTemplate.Triggers>
373383
<Trigger Property="IsChecked" Value="True">
374384
<Setter TargetName="Thumb" Property="Fill" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}" />
375-
<Setter TargetName="Track" Property="Fill" Value="{DynamicResource PrimaryHueLightBrush}" />
385+
<Setter TargetName="Track" Property="Fill" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(wpf:ToggleButtonAssist.SwitchTrackOnBackground)}" />
376386
<Setter Property="Foreground" Value="{DynamicResource PrimaryHueMidForegroundBrush}"/>
377387
</Trigger>
378388
<MultiTrigger>

MaterialDesignThemes.Wpf/ToggleButtonAssist.cs

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
using System.Windows;
1+
using System;
2+
using System.Windows;
23
using System.Windows.Controls.Primitives;
4+
using System.Windows.Media;
35

46
namespace MaterialDesignThemes.Wpf
57
{
68
public static class ToggleButtonAssist
79
{
10+
811
private static readonly DependencyPropertyKey HasOnContentPropertyKey =
912
DependencyProperty.RegisterAttachedReadOnly(
1013
"HasOnContent", typeof(bool), typeof(ToggleButtonAssist),
@@ -31,7 +34,7 @@ public static bool GetHasOnContent(DependencyObject element)
3134
/// Allows on (IsChecked) content to be provided on supporting <see cref="ToggleButton"/> styles.
3235
/// </summary>
3336
public static readonly DependencyProperty OnContentProperty = DependencyProperty.RegisterAttached(
34-
"OnContent", typeof (object), typeof (ToggleButtonAssist), new PropertyMetadata(default(object), OnContentPropertyChangedCallback));
37+
"OnContent", typeof(object), typeof(ToggleButtonAssist), new PropertyMetadata(default(object), OnContentPropertyChangedCallback));
3538

3639
private static void OnContentPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
3740
{
@@ -53,14 +56,14 @@ public static void SetOnContent(DependencyObject element, object value)
5356
/// </summary>
5457
public static object GetOnContent(DependencyObject element)
5558
{
56-
return (object) element.GetValue(OnContentProperty);
59+
return (object)element.GetValue(OnContentProperty);
5760
}
5861

5962
/// <summary>
6063
/// Allows an on (IsChecked) template to be provided on supporting <see cref="ToggleButton"/> styles.
6164
/// </summary>
6265
public static readonly DependencyProperty OnContentTemplateProperty = DependencyProperty.RegisterAttached(
63-
"OnContentTemplate", typeof (DataTemplate), typeof (ToggleButtonAssist), new PropertyMetadata(default(DataTemplate)));
66+
"OnContentTemplate", typeof(DataTemplate), typeof(ToggleButtonAssist), new PropertyMetadata(default(DataTemplate)));
6467

6568
/// <summary>
6669
/// Allows an on (IsChecked) template to be provided on supporting <see cref="ToggleButton"/> styles.
@@ -75,7 +78,35 @@ public static void SetOnContentTemplate(DependencyObject element, DataTemplate v
7578
/// </summary>
7679
public static DataTemplate GetOnContentTemplate(DependencyObject element)
7780
{
78-
return (DataTemplate) element.GetValue(OnContentTemplateProperty);
81+
return (DataTemplate)element.GetValue(OnContentTemplateProperty);
82+
}
83+
84+
public static DependencyProperty SwitchTrackOnBackgroundProperty =
85+
DependencyProperty.RegisterAttached(
86+
"SwitchTrackOnBackground", typeof(SolidColorBrush), typeof(ToggleButtonAssist));
87+
88+
public static void SetSwitchTrackOnBackground(DependencyObject element, SolidColorBrush value)
89+
{
90+
element.SetValue(SwitchTrackOnBackgroundProperty, value);
91+
}
92+
93+
public static SolidColorBrush GetSwitchTrackOnBackground(DependencyObject element)
94+
{
95+
return (SolidColorBrush)element.GetValue(SwitchTrackOnBackgroundProperty);
96+
}
97+
98+
public static DependencyProperty SwitchTrackOffBackgroundProperty =
99+
DependencyProperty.RegisterAttached(
100+
"SwitchTrackOffBackground", typeof(SolidColorBrush), typeof(ToggleButtonAssist));
101+
102+
public static void SetSwitchTrackOffBackground(DependencyObject element, SolidColorBrush value)
103+
{
104+
element.SetValue(SwitchTrackOffBackgroundProperty, value);
105+
}
106+
107+
public static SolidColorBrush GetSwitchTrackOffBackground(DependencyObject element)
108+
{
109+
return (SolidColorBrush)element.GetValue(SwitchTrackOffBackgroundProperty);
79110
}
80111
}
81112
}

0 commit comments

Comments
 (0)