Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
140
Event Handlers are called everytime tab changes
posted

We are using XamDockManager in our app.  We have found that everytime you select a tab, all the events in the sample are called afresh. 

ex. We have a combobox on tab "A" and we have hooked a combobox loaded event handler, evrytime we go from tab "A" is selected, the Combobox loaded event is called again.

This should only be called the first time the Tab is created?

  • 34510
    Verified Answer
    Offline posted

    Hello Elbino,

    This is actually the expected behavior.  The way the tabs work in the dockmanager is similar to how they work in the Microsoft TabControl.  Since only one tab can be in view, inside the template for the tab control there is a content area.  When a tab becomes selected, the tab's content gets placed inside this content area which in turn places the content into the Visual Tree.  Once the content is added to the Visual Tree, the Loaded event will fire.  When a new tab is selected, the old content is removed from the content area and new content takes its place.  This means the old tab content is taken out of the Visual Tree.  This back and forth will cause the Loaded event to fire multiple times.

    If you only want to handle code in the event once, you'll have to keep track of whether the content was loaded or not and execute the code accordingly.  For example: 

    private bool _isLoaded = false;
    
    private void ComboBox_Loaded(object sender, RoutedEventArgs e)
    {
        if (!_isLoaded)
        {
            // execute load code.
            // ...
            
            _isLoaded = true;
        }
    }