In C# Wpf, Why Is My Tabcontrol's Selectionchanged Event Firing Too Often

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

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 ;).

TabControl's SelectionChanged event issue

I think I need to take a rest, since my problem is really silly:

Turns out that instead of TabControl I should have used TabItem since it is the control I am interesting in.

So, my code has to be as below:

 void mainTabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.Source is TabItem)
{
if (this.IsLoaded)
{
//do work when tab is changed
}
}
}

SelectionChanged fired also on nested controls?

why happen this?

Because SelectionChanged is a routed event.

Routed Events Overview: https://learn.microsoft.com/en-us/dotnet/framework/wpf/advanced/routed-events-overview

You could use the OriginalSource property to determine whether a tab was selected:

private void MainTabs_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.OriginalSource == MainTabs)
{
//do your thing
}
}

tabcontrol selectionchanged event -- e.source issue

You have to convert e.source from an Object to a FrameworkElement, and then compare the types.

if (((FrameworkElement)e.Source).GetType()== typeof(System.Windows.Controls.TabControl))
{
if (item1.IsSelected)
{
myllist1.DataContext = getList1();
}
else if (item2.IsSelected)
{
mylist2.DataContext = getlist2();
}
else if (item3.IsSelected)
{
mylist3.DataContext = getlist3();
}
else if (item4.IsSelected)
{
mylist4.DataContext = getlist4();
}
}

Strange behaviour with TabControl in WPF on SelectionChanged

Ok, I found it. The MessageBox interrupts the change-event, so we have to use some other way. When you use a style, you can catch the change-event of the TabItems:

XAML:

    <TabControl Name="ConfigTabs" HorizontalAlignment="Left" VerticalAlignment="Top">
<TabControl.Resources>
<Style TargetType="TabItem">
<EventSetter Event="Selector.Selected" Handler="OnNewTabSelected"/>
</Style>
</TabControl.Resources>
<TabItem Header="Allgemeines">
...

</TabItem>
<TabItem Header="Monitorbelegung">
...
</TabItem>
<TabItem Header="Produkt-Konfigurationen">
...
</TabItem>
</TabControl>

C# (Code-Behind):

    private void OnNewTabSelected(object sender, RoutedEventArgs e)
{
if (e.Source is TabItem && this.IsLoaded)
{
TabItem MyTab = (TabItem)sender;
TabControl MyControl = (TabControl)MyTab.Parent;
if (MyControl.SelectedIndex == 0)
{
MessageBox.Show("Beep" + MyControl.SelectedIndex);
}
else if (MyControl.SelectedIndex == 1)
{
MessageBox.Show("Beep" + MyControl.SelectedIndex);
}
else if (MyControl.SelectedIndex == 2)
{
MessageBox.Show("Beep" + MyControl.SelectedIndex);
}
}
}

WPF ComboBox SelectionChanged event firing twice

What is causing this behavior?

The SelectionChanged event is raised initially when you enter the edit mode and the SelectedItem property is being bound to your source property.

How can the issue be fixed?

The easiest way to handle this is to check whether the ComboBox has been loaded and simply return from the event handler immediately if it hasn't:

private void MyComboBoxColumn_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox comboBox = (ComboBox)sender;
if (!comboBox.IsLoaded)
return;

//handle an actual selection here...
}


Related Topics



Leave a reply



Submit