Skip to content

Commit

Permalink
fix: #4462 raise toggle event correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
punker76 committed Feb 26, 2024
1 parent 87579a5 commit 7fb6f9d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@

<mah:ToggleSwitch Margin="{StaticResource ControlMargin}"
Header="Header"
Toggled="ToggleSwitch_OnToggled"
IsOn="{Binding CanUseToggleSwitch, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<mah:ToggleSwitch Margin="{StaticResource ControlMargin}"
CommandParameter="{Binding}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,10 @@ private void SplitButton_OnSelectionChanged(object sender, SelectionChangedEvent
var value = ((Selector)sender).SelectedValue;
Debug.WriteLine($">> SplitButton SelectionChanged: index={index}, item={item}, value={value}");
}

private void ToggleSwitch_OnToggled(object sender, RoutedEventArgs e)
{
Debug.WriteLine($">> ToggleSwitch Toggled: sender={sender}, OriginalSource={e.OriginalSource}, Source={e.Source}, IsOn={(sender as ToggleSwitch)?.IsOn}");
}
}
}
13 changes: 11 additions & 2 deletions src/MahApps.Metro/Controls/ToggleSwitch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -414,15 +414,24 @@ public bool IsPressed
set => this.SetValue(CommandTargetProperty, value);
}

/// <summary>
/// Toggled event
/// </summary>
public static readonly RoutedEvent ToggledEvent = EventManager.RegisterRoutedEvent(nameof(Toggled), RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ToggleSwitch));

/// <summary>
/// Occurs when "On"/"Off" state changes for this ToggleSwitch.
/// </summary>
public event RoutedEventHandler? Toggled;
public event RoutedEventHandler Toggled
{
add => this.AddHandler(ToggledEvent, value);
remove => this.RemoveHandler(ToggledEvent, value);
}

/// <summary>This method is invoked when the <see cref="IsOnProperty"/> changes.</summary>
protected virtual void OnToggled()
{
this.Toggled?.Invoke(this, new RoutedEventArgs());
this.RaiseEvent(new RoutedEventArgs(ToggledEvent));
}

static ToggleSwitch()
Expand Down

0 comments on commit 7fb6f9d

Please sign in to comment.