In C# WPF, why is my TabControl's SelectionChanged event firing too often?

C#WpfTabcontrolTabitem

C# Problem Overview


I have a tabbed GUI with each tab containing a Frame. In one of these Frames there is a DataGrid. When the user selects this tab, I need my datagrid sorted, so I'm using the TabControl SelectionChanged event to trigger the sort. However, this event triggers every time an item is selected from the DataGrid, even though the tabs themselves remain untouched.

I've tried number of different events: GotFocus for a TabItem RequestBringIntoView for a TabItem

but they all seem to suffer from this problem. What is causing this?

C# Solutions


Solution 1 - C#

The TabControl.SelectionChanged is the same event as a ComboBox.SelectionChanged

It originates from Selector.SelectionChanged.

So, if you do not mark your event as handled in your event handler, it will bubble up the tree, and eventually arrive at your TabControl, which is causing this "firing too often" issue.

Mark your event as handled in your SelectionChanged of your ComboBox/ListBox/ListView/any other Selector you use in your DataGrid like so:

private void MyComboBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
    e.Handled = true;
}

And this inconvenience will go away ;).

Solution 2 - C#

private void tabControlName_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.Source is TabControl) //if this event fired from TabControl then enter
{
if (tabItemName.IsSelected)
{
//Do your job here
}
}
}

Solution 3 - C#

If you have added a handler with AddHandler in a parent element, all selection changes will fire the SelectionChanged-event. In this case, you can give your TabControl a name and then check in the EventHandler if the name of the OriginalSource is the name of your TabControl.

Solution 4 - C#

Another good approch is adding a handler to the tabControl.Items.SelectionChanged:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
  ItemCollection view = tabControl.Items;
  view.CurrentChanged += new EventHandler(view_CurrentChanged);
}

void view_CurrentChanged(object sender, EventArgs e)
{
  throw new NotImplementedException();
}

Maybe is not the xamly way, but is less pain as it only fires when an item is changed.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndersView Question on Stackoverflow
Solution 1 - C#ArcturusView Answer on Stackoverflow
Solution 2 - C#TowhidView Answer on Stackoverflow
Solution 3 - C#HCLView Answer on Stackoverflow
Solution 4 - C#Shimmy WeitzhandlerView Answer on Stackoverflow