Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Changed the behaviour and design ot ToggleSwitch #3527

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@

<StackPanel Grid.Row="1"
Grid.Column="0"
HorizontalAlignment="Center">
HorizontalAlignment="Stretch">
<StackPanel.Resources>
<Style x:Key="CustomMetroToggleSwitch"
BasedOn="{StaticResource MahApps.Styles.ToggleSwitch}"
Expand All @@ -295,11 +295,11 @@
<Label Content="Toggle switch" Style="{DynamicResource DescriptionHeaderStyle}" />
<Controls:ToggleSwitch Margin="{StaticResource ControlMargin}"
Header="ToggleSwitch Header"
IsChecked="True"
IsChecked="{x:Null}"
IsThreeState="True"
ToggleSwitchButtonStyle="{StaticResource CustomMetroToggleSwitchButton}" />
<Controls:ToggleSwitch Margin="{StaticResource ControlMargin}"
IsChecked="True"
Style="{DynamicResource CustomMetroToggleSwitch}" />
IsChecked="True" />
<Controls:ToggleSwitch Margin="{StaticResource ControlMargin}"
IsChecked="True"
IsEnabled="False" />
Expand All @@ -308,15 +308,16 @@

<StackPanel Grid.Row="1"
Grid.Column="1"
HorizontalAlignment="Center">
HorizontalAlignment="Stretch">
<StackPanel.Resources>
<BooleanToVisibilityConverter x:Key="btv" />
</StackPanel.Resources>
<Label Content="Enable / Visible" Style="{DynamicResource DescriptionHeaderStyle}" />
<Controls:ToggleSwitch x:Name="enabledSwitch"
Margin="{StaticResource ControlMargin}"
Header="Win10 Style"
IsChecked="True"
IsChecked="{x:Null}"
IsThreeState="True"
OffLabel="Disabled"
OnLabel="Enabled"
Style="{StaticResource MahApps.Styles.ToggleSwitch.Win10}" />
Expand All @@ -334,7 +335,7 @@
Visibility="{Binding ElementName=visibleSwitch, Path=IsChecked, Converter={StaticResource btv}}" />

<Controls:ToggleSwitch Margin="{StaticResource ControlMargin}"
IsChecked="True"
IsChecked="{x:Null}"
IsEnabled="False"
Style="{StaticResource MahApps.Styles.ToggleSwitch.Win10}" />
<Controls:ToggleSwitch Margin="{StaticResource ControlMargin}"
Expand Down
116 changes: 99 additions & 17 deletions src/MahApps.Metro/Controls/ToggleSwitch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,27 @@ public class ToggleSwitch : HeaderedContentControl

public static readonly DependencyProperty HeaderFontFamilyProperty = DependencyProperty.Register("HeaderFontFamily", typeof(FontFamily), typeof(ToggleSwitch), new PropertyMetadata(SystemFonts.MessageFontFamily));

public static readonly DependencyProperty OnLabelProperty = DependencyProperty.Register("OnLabel", typeof(string), typeof(ToggleSwitch), new PropertyMetadata("On"));
public static readonly DependencyProperty OffLabelProperty = DependencyProperty.Register("OffLabel", typeof(string), typeof(ToggleSwitch), new PropertyMetadata("Off"));
public static readonly DependencyProperty OnLabelProperty = DependencyProperty.Register("OnLabel", typeof(object), typeof(ToggleSwitch), new PropertyMetadata("On"));
public static readonly DependencyProperty OffLabelProperty = DependencyProperty.Register("OffLabel", typeof(object), typeof(ToggleSwitch), new PropertyMetadata("Off"));
public static readonly DependencyProperty IntermediateLabelProperty = DependencyProperty.Register("IntermediateLabel", typeof(object), typeof(ToggleSwitch), new PropertyMetadata("Undefined"));

public static readonly DependencyProperty OnSwitchBrushProperty = DependencyProperty.Register("OnSwitchBrush", typeof(Brush), typeof(ToggleSwitch), null);
public static readonly DependencyProperty OffSwitchBrushProperty = DependencyProperty.Register("OffSwitchBrush", typeof(Brush), typeof(ToggleSwitch), null);
public static readonly DependencyProperty IntermediateSwitchBrushProperty = DependencyProperty.Register("IntermediateSwitchBrush", typeof(Brush), typeof(ToggleSwitch), null);

public static readonly DependencyProperty OnBorderBrushProperty = DependencyProperty.Register("OnBorderBrush", typeof(Brush), typeof(ToggleSwitch), null);
public static readonly DependencyProperty OffBorderBrushProperty = DependencyProperty.Register("OffBorderBrush", typeof(Brush), typeof(ToggleSwitch), null);
public static readonly DependencyProperty IntermediateBorderBrushProperty = DependencyProperty.Register("IntermediateBorderBrush", typeof(Brush), typeof(ToggleSwitch), null);

public static readonly DependencyProperty ThumbIndicatorOnBrushProperty = DependencyProperty.Register("ThumbIndicatorOnBrush", typeof(Brush), typeof(ToggleSwitch), null);
public static readonly DependencyProperty ThumbIndicatorOffBrushProperty = DependencyProperty.Register("ThumbIndicatorOffBrush", typeof(Brush), typeof(ToggleSwitch), null);
public static readonly DependencyProperty ThumbIndicatorIntermediateBrushProperty = DependencyProperty.Register("ThumbIndicatorIntermediateBrush", typeof(Brush), typeof(ToggleSwitch), null);

public static readonly DependencyProperty ThumbIndicatorBrushProperty = DependencyProperty.Register("ThumbIndicatorBrush", typeof(Brush), typeof(ToggleSwitch), null);
public static readonly DependencyProperty ThumbIndicatorDisabledBrushProperty = DependencyProperty.Register("ThumbIndicatorDisabledBrush", typeof(Brush), typeof(ToggleSwitch), null);
public static readonly DependencyProperty ThumbIndicatorWidthProperty = DependencyProperty.Register("ThumbIndicatorWidth", typeof(double), typeof(ToggleSwitch), new PropertyMetadata(13d));

public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.Register("IsChecked", typeof(bool?), typeof(ToggleSwitch), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.Journal, OnIsCheckedChanged));
public static readonly DependencyProperty IsThreeStateProperty = DependencyProperty.Register("IsThreeState", typeof(bool), typeof(ToggleSwitch), new PropertyMetadata(false));

public static readonly DependencyProperty CheckChangedCommandProperty = DependencyProperty.Register("CheckChangedCommand", typeof(ICommand), typeof(ToggleSwitch), new PropertyMetadata(null));
public static readonly DependencyProperty CheckedCommandProperty = DependencyProperty.Register("CheckedCommand", typeof(ICommand), typeof(ToggleSwitch), new PropertyMetadata(null));
Expand All @@ -55,7 +65,7 @@ public class ToggleSwitch : HeaderedContentControl
// LeftToRight means content left and button right and RightToLeft vise versa
public static readonly DependencyProperty ContentDirectionProperty = DependencyProperty.Register("ContentDirection", typeof(FlowDirection), typeof(ToggleSwitch), new PropertyMetadata(FlowDirection.LeftToRight));
/// <summary>
/// Identifies the <see cref="P:MahApps.Metro.Controls.ToggleSwitch.ContentPadding" /> dependency property.
/// Identifies the <see cref="P:MahApps.Metro.Controls.ToggleSwitch.ContentPadding" /> dependency property.
/// </summary>
public static readonly DependencyProperty ContentPaddingProperty = DependencyProperty.Register(nameof(ContentPadding), typeof(Thickness), typeof(ToggleSwitch), new FrameworkPropertyMetadata(new Thickness(), FrameworkPropertyMetadataOptions.AffectsParentMeasure));
public static readonly DependencyProperty ToggleSwitchButtonStyleProperty = DependencyProperty.Register("ToggleSwitchButtonStyle", typeof(Style), typeof(ToggleSwitch), new FrameworkPropertyMetadata(default(Style), FrameworkPropertyMetadataOptions.Inherits | FrameworkPropertyMetadataOptions.AffectsArrange | FrameworkPropertyMetadataOptions.AffectsMeasure));
Expand All @@ -78,7 +88,7 @@ public FontFamily HeaderFontFamily
/// <summary>
/// Gets/sets the text to display when the control is in it's On state.
/// </summary>
public string OnLabel
public object OnLabel
{
get { return (string)GetValue(OnLabelProperty); }
set { SetValue(OnLabelProperty, value); }
Expand All @@ -87,12 +97,22 @@ public string OnLabel
/// <summary>
/// Gets/sets the text to display when the control is in it's Off state.
/// </summary>
public string OffLabel
public object OffLabel
{
get { return (string)GetValue(OffLabelProperty); }
set { SetValue(OffLabelProperty, value); }
}


/// <summary>
/// Gets/sets the text to display when the control is in it's Intermediate state.
/// </summary>
public object IntermediateLabel
{
get { return (string)GetValue(IntermediateLabelProperty); }
set { SetValue(IntermediateLabelProperty , value); }
}

/// <summary>
/// Gets/sets the brush used for the on-switch's foreground.
/// </summary>
Expand All @@ -112,12 +132,67 @@ public Brush OffSwitchBrush
}

/// <summary>
/// Gets/sets the brush used for the thumb indicator.
/// Gets/sets the brush used for the intermediate-switch's foreground.
/// </summary>
public Brush IntermediateSwitchBrush
{
get { return (Brush)GetValue(IntermediateSwitchBrushProperty); }
set { SetValue(IntermediateSwitchBrushProperty, value); }
}


/// <summary>
/// Gets/sets the brush used for the on-switch's foreground.
/// </summary>
public Brush OnBorderBrush
{
get { return (Brush)GetValue(OnBorderBrushProperty); }
set { SetValue(OnBorderBrushProperty, value); }
}

/// <summary>
/// Gets/sets the brush used for the off-switch's foreground.
/// </summary>
public Brush OffBorderBrush
{
get { return (Brush)GetValue(OffBorderBrushProperty); }
set { SetValue(OffBorderBrushProperty, value); }
}

/// <summary>
/// Gets/sets the brush used for the intermediate-switch's foreground.
/// </summary>
public Brush IntermediateBorderBrush
{
get { return (Brush)GetValue(IntermediateBorderBrushProperty); }
set { SetValue(IntermediateBorderBrushProperty, value); }
}

/// <summary>
/// Gets/sets the brush used for the thumb indicator (on).
/// </summary>
public Brush ThumbIndicatorBrush
public Brush ThumbIndicatorOnBrush
{
get { return (Brush)GetValue(ThumbIndicatorBrushProperty); }
set { SetValue(ThumbIndicatorBrushProperty, value); }
get { return (Brush)GetValue(ThumbIndicatorOnBrushProperty); }
set { SetValue(ThumbIndicatorOnBrushProperty, value); }
}

/// <summary>
/// Gets/sets the brush used for the thumb indicator (off).
/// </summary>
public Brush ThumbIndicatorOffBrush
{
get { return (Brush)GetValue(ThumbIndicatorOffBrushProperty); }
set { SetValue(ThumbIndicatorOffBrushProperty, value); }
}

/// <summary>
/// Gets/sets the brush used for the thumb indicator (intermeidate).
/// </summary>
public Brush ThumbIndicatorIntermediateBrush
{
get { return (Brush)GetValue(ThumbIndicatorIntermediateBrushProperty); }
set { SetValue(ThumbIndicatorIntermediateBrushProperty, value); }
}

/// <summary>
Expand Down Expand Up @@ -177,6 +252,15 @@ public Style ToggleSwitchButtonStyle
set { SetValue(IsCheckedProperty, value); }
}

/// <summary>
/// Gets/sets whether the control allows IsThreeState or not.
/// </summary>
public bool IsThreeState
{
get { return (bool)GetValue(IsThreeStateProperty); }
set { SetValue(IsThreeStateProperty, value); }
}

/// <summary>
/// Gets/sets the command which will be executed if the IsChecked property was changed.
/// </summary>
Expand Down Expand Up @@ -300,21 +384,21 @@ public override void OnApplyTemplate()

_toggleButton.IsEnabledChanged -= IsEnabledHandler;

_toggleButton.PreviewMouseUp -= this.ToggleButtonPreviewMouseUp;
//_toggleButton.PreviewMouseUp -= this.ToggleButtonPreviewMouseUp;
}
_toggleButton = GetTemplateChild(SwitchPart) as ToggleButton;
if (_toggleButton != null)
{
_toggleButton.Checked += CheckedHandler;
_toggleButton.Unchecked += UncheckedHandler;
_toggleButton.Indeterminate += IndeterminateHandler;
_toggleButton.Click += ClickHandler;
//_toggleButton.Click += ClickHandler;
var binding = new Binding("IsChecked") { Source = this };
_toggleButton.SetBinding(ToggleButton.IsCheckedProperty, binding);

_toggleButton.IsEnabledChanged += IsEnabledHandler;

_toggleButton.PreviewMouseUp += this.ToggleButtonPreviewMouseUp;
//_toggleButton.PreviewMouseUp += this.ToggleButtonPreviewMouseUp;
}
ChangeVisualState(false);
}
Expand All @@ -337,7 +421,7 @@ private void CheckedHandler(object sender, RoutedEventArgs e)
{
command.Execute(commandParameter);
}

SafeRaise.Raise(Checked, this, e);
}

Expand Down Expand Up @@ -373,6 +457,4 @@ public override string ToString()
);
}
}
}


}