Tab switching and PreviewSelectionChanged

3 posts, 0 answers
  1. Rich
    Rich avatar
    2 posts
    Member since:
    Oct 2013

    Posted 30 Oct 2013 Link to this post

    I am working with multiple tabs in a silverlight app. I need to do what I think is a fairly simple operation. However it is causing me some problems. The scenario is as follows, when the user changes tabs, I need to check if there are any changes pending and prompt the user to either save them, discard them or cancel and stay in the tab. Furthermore, if they select save, and there are validation errors, I need to prompt the user of the problem and stay on the current tab.

    Seems pretty simple. Catch either the PreviewSelectionChanged event or the SelectionChanged event. Do the check and set handled = true if I need to stay on the current tab. The problem is, the confirmation box that I am using doesn't block the UI thread. So I have to set handled=true to cancel out of the change, then change the tab programatically after the user makes their selection. I have tried this pretty much every way I can. Here is how I currently have the code:

    private void changeTab()
        tabControl.SelectedIndex = selectedTab;
    bool checkForSave = true;
    int currentTab = 0;
    int selectedTab = 0;
    private void tabControl_PreviewSelectionChanged(object sender, RadSelectionChangedEventArgs ar)
        if (currentDataGrid == null)
            currentTab = tabControl.SelectedIndex;
        if (checkForSave)
            ar.Handled = true;
            selectedTab = tabControl.SelectedIndex;
            tabControl.SelectedIndex = currentTab;
            //This method does the confirmation, sets checkForSave = false and calls the changeTab method.
            currentTab = tabControl.SelectedIndex;
            checkForSave = false;

    I'm stepping through the code and it is working correctly. There are a couple issues:

    1. Even though I am setting handled = true, the selectedIndex of the tab control stays with the new tab the user selected.To combat this, I try resetting the selectedIndex back to the previous value.
    2. When I set the selectedIndex on the tabControl in the changeTab method above it doesn't work.

    I know there has to be some other way of doing this. The main problem I have is that the confirmation box isn't blocking the UI thread (as it shouldn't) but this means that the tab switching has already occured when the user disposes of the confirmation box. 

  2. Rich
    Rich avatar
    2 posts
    Member since:
    Oct 2013

    Posted 02 Nov 2013 Link to this post

    Any help on this from the Telerik folks???
  3. Tina Stancheva
    Tina Stancheva avatar
    3299 posts

    Posted 04 Nov 2013 Link to this post

    Hi Rich,

    The PreviewSelectionChanged event is not truly fired before starting a selection change - it is rather an event that is fired before the selection process is complete and it lets you revert the selection changes back. This means that when you handle a PreviewSelectionChanged event it starts reverting the selection. At some point during that asynchronous operation you try to reset the selection (in case the current RadTabItem is validated), but as there are selection changes currently being executed, the manual selection fails.

    In order to workaround this, you will have to wrap your manual selection in a Dispatcher to delay its execution. You can try something in the lines of this:
    Dispatcher.BeginInvoke(() => {this.changeTab(); });

    Another approach you can use is to place your selection logic in the ViewModel. If you bind the IsSelected property of each RadTabItem to a business bool property, you can add business logic to control whether the value of the property should be changed or not. This approach works better in MVVM scenarios and I would recommend giving it a try if your RadTabControl ItemSource is data-bound to a business collection.

    On a side note, please have in mind that posting questions to our forums is a good way to get feedback from others in the community, but it doesn’t come with a guaranteed response time from our staff like support tickets do. This is why we highly recommend using the support ticketing system when you need a timely response to urgent issues.

    Tina Stancheva
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
Back to Top