Skip to content

Commit f5766d8

Browse files
authored
Allow for resizing the ToggleButton by setting the Width (#2977)
1 parent 6c3bb3b commit f5766d8

File tree

3 files changed

+91
-89
lines changed

3 files changed

+91
-89
lines changed

MainDemo.Wpf/Toggles.xaml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<UserControl x:Class="MaterialDesignDemo.Toggles"
1+
<UserControl x:Class="MaterialDesignDemo.Toggles"
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
@@ -264,7 +264,9 @@
264264
<smtx:XamlDisplay Margin="8,0,0,0"
265265
VerticalAlignment="Center"
266266
UniqueKey="buttons_64">
267-
<ToggleButton Style="{StaticResource MaterialDesignSwitchToggleButton}" ToolTip="MaterialDesignSwitchToggleButton with Custom Track Background">
267+
<ToggleButton Style="{StaticResource MaterialDesignSwitchToggleButton}"
268+
Width="60"
269+
ToolTip="MaterialDesignSwitchToggleButton with Custom Track Background">
268270
<materialDesign:ToggleButtonAssist.SwitchTrackOnBackground>
269271
<SolidColorBrush Color="Green" />
270272
</materialDesign:ToggleButtonAssist.SwitchTrackOnBackground>
@@ -365,10 +367,10 @@
365367
<TextBlock Text="ListBoxAssist.IsToggle allows more natural toggle behaviour" />
366368
</StackPanel>
367369
</ListBox.ToolTip>
368-
<ListBoxItem Content="{materialDesign:PackIcon Kind=FormatBold}" />
369-
<ListBoxItem Content="{materialDesign:PackIcon Kind=FormatItalic}" />
370-
<ListBoxItem Content="{materialDesign:PackIcon Kind=FormatUnderline}" />
371-
</ListBox>
370+
<ListBoxItem Content="{materialDesign:PackIcon Kind=FormatBold}" />
371+
<ListBoxItem Content="{materialDesign:PackIcon Kind=FormatItalic}" />
372+
<ListBoxItem Content="{materialDesign:PackIcon Kind=FormatUnderline}" />
373+
</ListBox>
372374
</smtx:XamlDisplay>
373375

374376

MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.ToggleButton.xaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@
348348
</Style>
349349

350350
<Style x:Key="MaterialDesignSwitchToggleButton" TargetType="{x:Type ToggleButton}">
351+
<Setter Property="Width" Value="34" />
351352
<Setter Property="Background" Value="{DynamicResource PrimaryHueMidBrush}" />
352353
<Setter Property="BorderThickness" Value="1" />
353354
<Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}" />
@@ -385,7 +386,7 @@
385386
Duration="0:0:0.3" />
386387
</Storyboard>
387388
</ControlTemplate.Resources>
388-
<Viewbox Width="34">
389+
<Viewbox Width="{TemplateBinding Width}">
389390
<VisualStateManager.VisualStateGroups>
390391
<VisualStateGroup x:Name="CheckStates">
391392
<VisualStateGroup.Transitions>
Lines changed: 81 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,85 @@
11
using System.Windows.Media;
22

3-
namespace MaterialDesignThemes.Wpf
3+
namespace MaterialDesignThemes.Wpf;
4+
5+
public static class ToggleButtonAssist
46
{
5-
public static class ToggleButtonAssist
6-
{
7-
private static readonly DependencyPropertyKey HasOnContentPropertyKey =
8-
DependencyProperty.RegisterAttachedReadOnly(
9-
"HasOnContent", typeof(bool), typeof(ToggleButtonAssist),
10-
new PropertyMetadata(false));
11-
12-
public static readonly DependencyProperty HasOnContentProperty = HasOnContentPropertyKey.DependencyProperty;
13-
14-
private static void SetHasOnContent(DependencyObject element, object value)
15-
=> element.SetValue(HasOnContentPropertyKey, value);
16-
17-
/// <summary>
18-
/// Framework use only.
19-
/// </summary>
20-
/// <param name="element"></param>
21-
/// <returns></returns>
22-
public static bool GetHasOnContent(DependencyObject element)
23-
=> (bool)element.GetValue(HasOnContentProperty);
24-
25-
/// <summary>
26-
/// Allows on (IsChecked) content to be provided on supporting <see cref="ToggleButton"/> styles.
27-
/// </summary>
28-
public static readonly DependencyProperty OnContentProperty = DependencyProperty.RegisterAttached(
29-
"OnContent", typeof(object), typeof(ToggleButtonAssist), new PropertyMetadata(default(object), OnContentPropertyChangedCallback));
30-
31-
private static void OnContentPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
32-
=> SetHasOnContent(dependencyObject, dependencyPropertyChangedEventArgs.NewValue != null);
33-
34-
/// <summary>
35-
/// Allows on (IsChecked) content to be provided on supporting <see cref="ToggleButton"/> styles.
36-
/// </summary>
37-
/// <param name="element"></param>
38-
/// <param name="value"></param>
39-
public static void SetOnContent(DependencyObject element, object value)
40-
=> element.SetValue(OnContentProperty, value);
41-
42-
/// <summary>
43-
/// Allows on (IsChecked) content to be provided on supporting <see cref="ToggleButton"/> styles.
44-
/// </summary>
45-
public static object GetOnContent(DependencyObject element)
46-
=> element.GetValue(OnContentProperty);
47-
48-
/// <summary>
49-
/// Allows an on (IsChecked) template to be provided on supporting <see cref="ToggleButton"/> styles.
50-
/// </summary>
51-
public static readonly DependencyProperty OnContentTemplateProperty = DependencyProperty.RegisterAttached(
52-
"OnContentTemplate", typeof(DataTemplate), typeof(ToggleButtonAssist), new PropertyMetadata(default(DataTemplate)));
53-
54-
/// <summary>
55-
/// Allows an on (IsChecked) template to be provided on supporting <see cref="ToggleButton"/> styles.
56-
/// </summary>
57-
public static void SetOnContentTemplate(DependencyObject element, DataTemplate value)
58-
=> element.SetValue(OnContentTemplateProperty, value);
59-
60-
/// <summary>
61-
/// Allows an on (IsChecked) template to be provided on supporting <see cref="ToggleButton"/> styles.
62-
/// </summary>
63-
public static DataTemplate GetOnContentTemplate(DependencyObject element)
64-
=> (DataTemplate)element.GetValue(OnContentTemplateProperty);
65-
66-
public static readonly DependencyProperty SwitchTrackOnBackgroundProperty =
67-
DependencyProperty.RegisterAttached(
68-
"SwitchTrackOnBackground", typeof(SolidColorBrush), typeof(ToggleButtonAssist));
69-
70-
public static void SetSwitchTrackOnBackground(DependencyObject element, SolidColorBrush value)
71-
=> element.SetValue(SwitchTrackOnBackgroundProperty, value);
72-
73-
public static SolidColorBrush GetSwitchTrackOnBackground(DependencyObject element)
74-
=> (SolidColorBrush)element.GetValue(SwitchTrackOnBackgroundProperty);
75-
76-
public static readonly DependencyProperty SwitchTrackOffBackgroundProperty =
77-
DependencyProperty.RegisterAttached(
78-
"SwitchTrackOffBackground", typeof(SolidColorBrush), typeof(ToggleButtonAssist));
79-
80-
public static void SetSwitchTrackOffBackground(DependencyObject element, SolidColorBrush value)
81-
=> element.SetValue(SwitchTrackOffBackgroundProperty, value);
82-
83-
public static SolidColorBrush GetSwitchTrackOffBackground(DependencyObject element)
84-
=> (SolidColorBrush)element.GetValue(SwitchTrackOffBackgroundProperty);
85-
}
7+
private static readonly DependencyPropertyKey HasOnContentPropertyKey =
8+
DependencyProperty.RegisterAttachedReadOnly(
9+
"HasOnContent", typeof(bool), typeof(ToggleButtonAssist),
10+
new PropertyMetadata(false));
11+
12+
public static readonly DependencyProperty HasOnContentProperty = HasOnContentPropertyKey.DependencyProperty;
13+
14+
private static void SetHasOnContent(DependencyObject element, object value)
15+
=> element.SetValue(HasOnContentPropertyKey, value);
16+
17+
/// <summary>
18+
/// Framework use only.
19+
/// </summary>
20+
/// <param name="element"></param>
21+
/// <returns></returns>
22+
public static bool GetHasOnContent(DependencyObject element)
23+
=> (bool)element.GetValue(HasOnContentProperty);
24+
25+
/// <summary>
26+
/// Allows on (IsChecked) content to be provided on supporting <see cref="ToggleButton"/> styles.
27+
/// </summary>
28+
public static readonly DependencyProperty OnContentProperty = DependencyProperty.RegisterAttached(
29+
"OnContent", typeof(object), typeof(ToggleButtonAssist), new PropertyMetadata(default(object), OnContentPropertyChangedCallback));
30+
31+
private static void OnContentPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
32+
=> SetHasOnContent(dependencyObject, dependencyPropertyChangedEventArgs.NewValue != null);
33+
34+
/// <summary>
35+
/// Allows on (IsChecked) content to be provided on supporting <see cref="ToggleButton"/> styles.
36+
/// </summary>
37+
/// <param name="element"></param>
38+
/// <param name="value"></param>
39+
public static void SetOnContent(DependencyObject element, object value)
40+
=> element.SetValue(OnContentProperty, value);
41+
42+
/// <summary>
43+
/// Allows on (IsChecked) content to be provided on supporting <see cref="ToggleButton"/> styles.
44+
/// </summary>
45+
public static object GetOnContent(DependencyObject element)
46+
=> element.GetValue(OnContentProperty);
47+
48+
/// <summary>
49+
/// Allows an on (IsChecked) template to be provided on supporting <see cref="ToggleButton"/> styles.
50+
/// </summary>
51+
public static readonly DependencyProperty OnContentTemplateProperty = DependencyProperty.RegisterAttached(
52+
"OnContentTemplate", typeof(DataTemplate), typeof(ToggleButtonAssist), new PropertyMetadata(default(DataTemplate)));
53+
54+
/// <summary>
55+
/// Allows an on (IsChecked) template to be provided on supporting <see cref="ToggleButton"/> styles.
56+
/// </summary>
57+
public static void SetOnContentTemplate(DependencyObject element, DataTemplate value)
58+
=> element.SetValue(OnContentTemplateProperty, value);
59+
60+
/// <summary>
61+
/// Allows an on (IsChecked) template to be provided on supporting <see cref="ToggleButton"/> styles.
62+
/// </summary>
63+
public static DataTemplate GetOnContentTemplate(DependencyObject element)
64+
=> (DataTemplate)element.GetValue(OnContentTemplateProperty);
65+
66+
public static readonly DependencyProperty SwitchTrackOnBackgroundProperty =
67+
DependencyProperty.RegisterAttached(
68+
"SwitchTrackOnBackground", typeof(SolidColorBrush), typeof(ToggleButtonAssist));
69+
70+
public static void SetSwitchTrackOnBackground(DependencyObject element, SolidColorBrush value)
71+
=> element.SetValue(SwitchTrackOnBackgroundProperty, value);
72+
73+
public static SolidColorBrush GetSwitchTrackOnBackground(DependencyObject element)
74+
=> (SolidColorBrush)element.GetValue(SwitchTrackOnBackgroundProperty);
75+
76+
public static readonly DependencyProperty SwitchTrackOffBackgroundProperty =
77+
DependencyProperty.RegisterAttached(
78+
"SwitchTrackOffBackground", typeof(SolidColorBrush), typeof(ToggleButtonAssist));
79+
80+
public static void SetSwitchTrackOffBackground(DependencyObject element, SolidColorBrush value)
81+
=> element.SetValue(SwitchTrackOffBackgroundProperty, value);
82+
83+
public static SolidColorBrush GetSwitchTrackOffBackground(DependencyObject element)
84+
=> (SolidColorBrush)element.GetValue(SwitchTrackOffBackgroundProperty);
8685
}

0 commit comments

Comments
 (0)