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

ToggleSwitch "Click" event not triggered #2415 #2421

Merged
merged 1 commit into from
Mar 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion MahApps.Metro/Controls/ToggleSwitch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using MahApps.Metro.Converters;

namespace MahApps.Metro.Controls
{
Expand Down
38 changes: 21 additions & 17 deletions MahApps.Metro/Controls/ToggleSwitchButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class ToggleSwitchButton : ToggleButton
private Grid _SwitchTrack;
private Shape _ThumbIndicator;
private TranslateTransform _ThumbTranslate;
private readonly PropertyChangeNotifier isCheckedPropertyChangeNotifier;

[Obsolete(@"This property will be deleted in the next release. You should use OnSwitchBrush and OffSwitchBrush to change the switch's brushes.")]
public static readonly DependencyProperty SwitchForegroundProperty = DependencyProperty.Register("SwitchForeground", typeof(Brush), typeof(ToggleSwitchButton), new PropertyMetadata(null, (o, e) => ((ToggleSwitchButton)o).OnSwitchBrush = e.NewValue as Brush));
Expand Down Expand Up @@ -98,11 +99,11 @@ public double ThumbIndicatorWidth
public ToggleSwitchButton()
{
DefaultStyleKey = typeof(ToggleSwitchButton);
Checked += ToggleSwitchButton_Checked;
Unchecked += ToggleSwitchButton_Checked;
isCheckedPropertyChangeNotifier = new PropertyChangeNotifier(this, ToggleSwitchButton.IsCheckedProperty);
isCheckedPropertyChangeNotifier.ValueChanged += IsCheckedPropertyChangeNotifierValueChanged;
}

void ToggleSwitchButton_Checked(object sender, RoutedEventArgs e)
private void IsCheckedPropertyChangeNotifierValueChanged(object sender, EventArgs e)
{
UpdateThumb();
}
Expand Down Expand Up @@ -132,12 +133,6 @@ private void UpdateThumb()
}
}

protected override void OnToggle()
{
IsChecked = IsChecked != true;
UpdateThumb();
}

public override void OnApplyTemplate()
{
base.OnApplyTemplate();
Expand All @@ -147,21 +142,27 @@ public override void OnApplyTemplate()
_ThumbIndicator = GetTemplateChild(PART_ThumbIndicator) as Shape;
_ThumbTranslate = GetTemplateChild(PART_ThumbTranslate) as TranslateTransform;

if (_ThumbIndicator != null && _BackgroundTranslate != null)
if (_ThumbIndicator != null && _ThumbTranslate != null && _BackgroundTranslate != null)
{
Binding translationBinding;
translationBinding = new System.Windows.Data.Binding("X");
translationBinding.Source = _ThumbTranslate;
BindingOperations.SetBinding(_BackgroundTranslate, TranslateTransform.XProperty, translationBinding);
}

if (_DraggingThumb != null && _SwitchTrack != null && _ThumbIndicator != null && _ThumbTranslate != null)
if (_DraggingThumb != null && _ThumbIndicator != null && _ThumbTranslate != null)
{
_DraggingThumb.DragStarted -= _DraggingThumb_DragStarted;
_DraggingThumb.DragDelta -= _DraggingThumb_DragDelta;
_DraggingThumb.DragCompleted -= _DraggingThumb_DragCompleted;
_DraggingThumb.DragStarted += _DraggingThumb_DragStarted;
_DraggingThumb.DragDelta += _DraggingThumb_DragDelta;
_DraggingThumb.DragCompleted += _DraggingThumb_DragCompleted;

_SwitchTrack.SizeChanged += _SwitchTrack_SizeChanged;
if (_SwitchTrack != null)
{
_SwitchTrack.SizeChanged -= _SwitchTrack_SizeChanged;
_SwitchTrack.SizeChanged += _SwitchTrack_SizeChanged;
}
}
}

Expand Down Expand Up @@ -199,19 +200,22 @@ void _DraggingThumb_DragCompleted(object sender, DragCompletedEventArgs e)
_lastDragPosition = null;
if (!_isDragging)
{
OnToggle();
OnClick();
}
else if (_ThumbTranslate != null && _SwitchTrack != null)
{
if (!IsChecked.GetValueOrDefault() && _ThumbTranslate.X + 6.5 >= _SwitchTrack.ActualWidth / 2)
{
OnToggle();
OnClick();
}
else if (IsChecked.GetValueOrDefault() && _ThumbTranslate.X + 6.5 <= _SwitchTrack.ActualWidth / 2)
{
OnToggle();
OnClick();
}
else
{
UpdateThumb();
}
else UpdateThumb();
}
}

Expand Down
4 changes: 2 additions & 2 deletions MahApps.Metro/Themes/ToggleSwitch.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@
</Rectangle.RenderTransform>
</Rectangle>
</Grid>
<Thumb x:Name="PART_DraggingThumb">
<Thumb x:Name="PART_DraggingThumb" Background="Transparent">
<Thumb.Template>
<ControlTemplate>
<Rectangle Fill="Transparent" />
<Grid Background="{TemplateBinding Background}" />
</ControlTemplate>
</Thumb.Template>
</Thumb>
Expand Down