Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8470773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:36:50+00:00 2026-06-10T16:36:50+00:00

I have a tab control on a page; its items are bound back to

  • 0

I have a tab control on a page; its items are bound back to my ViewModel, which also exposes an ActiveTabItemIndex which is bound (two way) to the SelectedIndex property in my xaml, and which implements INotifyPropertyChanged so that my TabControl knows when to update.

This is (I understand) the MVVM-correct way to do things, and works 99% properly.

class MainWindowViewModel : BaseViewModel, INotifyPropertyChanged
{
    ObservableCollection<TabItemViewModel> _TabItems;
    int _ActiveTabItemIndex;

    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(name));
    }

    void _TabItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
            _ActiveTabItemIndex = _TabItems.IndexOf((TabItemViewModel)e.NewItems[0]);
        RaisePropertyChanged("ActiveTabItemIndex");
    }

    public ObservableCollection<TabItemViewModel> TabItems
    {
        get
        {
            if (_TabItems == null)
            {
                _TabItems = new ObservableCollection<TabItemViewModel>();
                _TabItems.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(_TabItems_CollectionChanged);
            }
            return _TabItems;
        }
    }

    public int ActiveTabItemIndex
    {
        get
        {
            return _ActiveTabItemIndex;
        }
        set
        {
            _ActiveTabItemIndex = value;
        }
    }
}

This way any manipulations I make to my TabItems collection are reflected on the TabControl, and when I add a new item, it is automatically selected. This works a treat; however, when adding the very first item to an empty tab control, it looks like this:

Tab contents displayed but tab not selected

The tab contents are displayed, but the tab is not selected. I need to manually click the tab to get it to look right:

Tab contents displayed and tab selected

It is as though there is some kind of disconnect between the drawing of the tabs and the drawing of their contents. I know the binding is working because subsequent tabs are handled correctly and if I remove the binding altogether then the first page does not show its contents until the tab is manually selected. If anyone has seen this or can shed some light it would be very much appreciated! Thank you all 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T16:36:51+00:00Added an answer on June 10, 2026 at 4:36 pm

    Only raise your property change events in the setter; you can think of it as allowing the property itself to dictate what “changed” means, and by extension, giving it control over when the event is fired (and makes it do what you expect):

    public int ActiveTabItemIndex
    {
        get{ return _ActiveTabItemIndex; }
        set
        {
            if(_ActiveTabItemIndex != value)
            {
                _ActiveTabItemIndex = value;
                RaisePropertyChanged("ActiveTabItemIndex");
            }
        }
    }
    

    Just change

    _ActiveTabItemIndex = _TabItems.IndexOf(...);
    

    to

    ActiveTabItemIndex = _TabItems.IndexOf(...);
    

    and remove the RaisePropertyChanged call from _TabItems_CollectionChanged

    There will be times when you’ll need raise property changed notifications outside a property’s setter but that is for a far more complicated day 🙂

    As an aside, INotifyPropertyChanged should be implemented on your BaseViewModel. Check out the absolutely fantastic MVVM Light Toolkit – it has all the code you’d otherwise have to duplicate in every project that you use MVVM in.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jQueryUI Tabs control on a web page. Each tab contains one
I have a tab control with two different potential item templates: <TabControl ItemTemplateSelector={StaticResource tabTemplateSelector}/>
I have a tab control bound to an observablecollection for dynamic tabs as follows:
I have a form which has only tab control in it. I want to
I have a web application (ASP.NET MVC3) which uses the jquery ui tab control
I have a html page with a basic tab control. I use javascript to
I have built two control containers on a page, one is using jquery-ui tabs
I have a custom text box control which raises a routed event when its
I have a UltraTree control which selects a page to display in a UltraTabControl.
I have a page with a tab control and each control has almost 15

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.