0

i have a problem with the Tab Control, the SelectionChanged is firing multiple time either if i did not change the tab, this is my code:

    private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        int tabItem = ((sender as TabControl)).SelectedIndex;
        if (e.Source is TabControl) // This is a soultion of those problem.
        {
            switch (tabItem)
            {
                case 0:
                       //something
                       break;
                case 1:
                       //something
                       break;
            }
    }

Can someone help me to solve this issue?

Thanks a lot

dadou
  • 45
  • 1
  • 11
  • "Always"? What does this mean in this context? Your event handler should be fired whenever there is a SelectionChanged event raise from the TabControl. – mm8 Sep 17 '18 at 13:46
  • Sorry i edited my question, it's not always, but it fired multiple times – dadou Sep 17 '18 at 13:53
  • Do you trigger the event manually in another function? – carapaece Sep 17 '18 at 13:54
  • Once for everty SelectionChanged event? That's expected. What is your issue? – mm8 Sep 17 '18 at 13:56
  • The event is triggered by the mouse. I saw in many post that the Selectionchanged event is part of Selector.SelectionChanged and i need to set my event to handled but i can't understand how to do. – dadou Sep 17 '18 at 14:03

2 Answers2

1

Do you have any procedure that refresh the view? When you refresh the view its execute the selectionchanged event. I use your code in my own tabcontrol and it works fine.

try to use this event

private void TabControl_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        int tabItem = ((sender as TabControl)).SelectedIndex;
        if (e.Source is TabControl) // This is a soultion of those problem.
        {
            switch (tabItem)
            {
                case 0:
                    //something
                    break;
                case 1:
                    //something
                    break;
            }
        }
    }
  • Thanks you for your answer but it's not working for me. I think i know why the problem happens, I have 2 Tab Control in the same windows. Whan i'm switching Tab in the second one, it fired the first Tab Control Selection_Changed event. – dadou Sep 20 '18 at 05:18
0

I finally found a solution:

private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
        if (ReferenceEquals(e.OriginalSource, this.tab_main))
        {
            if (tab_type.IsSelected)
            {
               //something
            }

            else if (tab_problem.IsSelected)
            {
               //something
            }
         }
}

I found this solution according the comment of HiredMind in this post: https://stackoverflow.com/a/3659889/6688895

i'm quoting his answer

if you're encountering this: don't just check OriginalSource's type - check to make sure OriginalSource actually refers to your particular TabControl: "if (ReferenceEquals(e.OriginalSource, this.myTabControl)". If you don't, then all child tab controls will activate your event handler code. – HiredMind Aug 6 '13 at 15:12

dadou
  • 45
  • 1
  • 11