Skip to content

Commit

Permalink
(GH-4012) Fix FlipView SelectionChanged event firing twice
Browse files Browse the repository at this point in the history
The SelectionChanged event is a bubbling event, so if the ListBox inside the FlipView fires the event, all other events in the same container will be fired.
  • Loading branch information
punker76 committed Feb 27, 2021
1 parent 4836329 commit 5e17639
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/MahApps.Metro/Controls/FlipView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace MahApps.Metro.Controls
[TemplatePart(Name = PART_DownButton, Type = typeof(Button))]
[TemplatePart(Name = PART_BannerGrid, Type = typeof(Grid))]
[TemplatePart(Name = PART_BannerLabel, Type = typeof(Label))]
[TemplatePart(Name = PART_Index, Type = typeof(ListBox))]
[StyleTypedProperty(Property = nameof(NavigationButtonStyle), StyleTargetType = typeof(Button))]
[StyleTypedProperty(Property = nameof(IndexItemContainerStyle), StyleTargetType = typeof(ListBoxItem))]
public class FlipView : Selector
Expand Down Expand Up @@ -696,6 +697,7 @@ public string ButtonDownContentStringFormat
private const string PART_ForwardButton = "PART_ForwardButton";
private const string PART_Presenter = "PART_Presenter";
private const string PART_UpButton = "PART_UpButton";
private const string PART_Index = "PART_Index";
/// <summary>
/// To counteract the double Loaded event issue.
/// </summary>
Expand Down Expand Up @@ -911,6 +913,20 @@ public override void OnApplyTemplate()
{
this.bannerLabel.Opacity = this.IsBannerEnabled ? 1d : 0d;
}

this.ExecuteWhenLoaded(() =>
{
if (this.GetTemplateChild(PART_Index) is ListBox listBox)
{
listBox.SelectionChanged += (sender, e) =>
{
if (ReferenceEquals(e.OriginalSource, listBox))
{
e.Handled = true;
}
};
}
});
}

protected override DependencyObject GetContainerForItemOverride()
Expand Down

0 comments on commit 5e17639

Please sign in to comment.