---
title: "WPF SelectedIndex set issue of TabControl in WPF"  
description: "WPF SelectedIndex set issue of TabControl in WPF"  
author: "Andrew Deniel"  
published: 2013-07-18  
updated: 2013-07-18  
canonical: https://www.mindstick.com/forum/1319/wpf-selectedindex-set-issue-of-tabcontrol-in-wpf  
category: "wpf"  
tags: ["wpf"]  
reading_time: 1 minute  

---

# WPF SelectedIndex set issue of TabControl in WPF

Hi experts!

I have a TabControl with two items.

<TabControl x:Name="tab" SelectionChanged="TabControl_SelectionChanged">

<TabItem [Header](https://www.mindstick.com/articles/336036/explain-about-html-header-body-and-footer-tags)="TabItem1">

<Grid />

</TabItem>

<TabItem Header="TabItem2">

<Grid />

</TabItem>

</TabControl>

[private](https://www.mindstick.com/blog/11097/java-access-modifiers-the-public-and-the-private-modifiers) void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)

{

Debug.WriteLine("Selected [Index](https://www.mindstick.com/blog/198/index-in-sql-server): " + tab.SelectedIndex);

if (tab.SelectedIndex == 1)

{

tab.SelectedIndex = 0;

}

}

when [click](https://www.mindstick.com/articles/12423/social-login-magento-2-one-click-to-register-social) 2nd item, 1st item have [focus](https://yourviews.mindstick.com/audio/1251/how-music-can-improve-your-focus-and-mood) and [print](https://www.mindstick.com/blog/301752/types-of-3d-printing-technology) below.

Selected Index: 1

Selected Index: 0

but retry clicking 2nd item, no [output](https://www.mindstick.com/interview/34427/explain-the-output-in-angular)! SelectionChanged [event](https://www.mindstick.com/articles/167719/marquee-hire-benefits-of-event-lighting-hire-london) do not fire.

what's [wrong](https://answers.mindstick.com/qa/48468/who-wrote-the-the-wrong-enemy-america-in-afghanistan-2001-2014-and-when)? Is there work around?

thanks in [advance](https://www.mindstick.com/blog/33258/jee-mains-and-jee-advance-exams)

## Replies

### Reply by shreesh chandra shukla

Solution!

This is because you are changing the selectedIndex within the SelcetedIndexChanged event which will call itself in sycnhronous manner. Instead try to put it on UI dispatcher in an aysnchronous manner like this -

```
private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e){   Debug.WriteLine("Selected Index: " + tab.SelectedIndex);   if (tab.SelectedIndex == 1)   {      Application.Current.Dispatcher.BeginInvoke          ((Action)delegate { tab.SelectedIndex = 0; }, DispatcherPriority.Render, null);   }}
```

It will give you the desired output.


---

Original Source: https://www.mindstick.com/forum/1319/wpf-selectedindex-set-issue-of-tabcontrol-in-wpf

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
