Skip to content

Commit

Permalink
(MahAppsGH-4012) Register FlipView ListBox index SelectionChanged eve…
Browse files Browse the repository at this point in the history
…nt only once
  • Loading branch information
punker76 authored and batzen committed Mar 3, 2021
1 parent 8682b5d commit 533eb5f
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/MahApps.Metro/Controls/FlipView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,7 @@ public string ButtonDownContentStringFormat
private bool allowSelectedIndexChangedCallback = true;
private Grid bannerGrid;
private Label bannerLabel;
private ListBox indexListBox;
private Button backButton;
private Button forwardButton;
private Button downButton;
Expand Down Expand Up @@ -861,6 +862,11 @@ public override void OnApplyTemplate()

this.presenter = this.GetTemplateChild(PART_Presenter) as TransitioningContentControl;

if (this.indexListBox != null)
{
this.indexListBox.SelectionChanged -= OnIndexListBoxSelectionChanged;
}

if (this.forwardButton != null)
{
this.forwardButton.Click -= this.NextButtonClick;
Expand All @@ -881,6 +887,8 @@ public override void OnApplyTemplate()
this.downButton.Click -= this.NextButtonClick;
}

this.indexListBox = this.GetTemplateChild(PART_Index) as ListBox;

this.forwardButton = this.GetTemplateChild(PART_ForwardButton) as Button;
this.backButton = this.GetTemplateChild(PART_BackButton) as Button;
this.upButton = this.GetTemplateChild(PART_UpButton) as Button;
Expand Down Expand Up @@ -913,22 +921,24 @@ public override void OnApplyTemplate()
{
this.bannerLabel.Opacity = this.IsBannerEnabled ? 1d : 0d;
}

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

private void OnIndexListBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (ReferenceEquals(e.OriginalSource, this.indexListBox))
{
e.Handled = true;
}
}

protected override DependencyObject GetContainerForItemOverride()
{
return new FlipViewItem() { HorizontalAlignment = HorizontalAlignment.Stretch };
Expand Down

0 comments on commit 533eb5f

Please sign in to comment.