This is a migrated thread and some comments may be shown as answers.

Tab switching and PreviewSelectionChanged

2 Answers 320 Views
TabControl
This is a migrated thread and some comments may be shown as answers.
Rich
Top achievements
Rank 1
Rich asked on 31 Oct 2013, 03:12 AM
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;
        return;
    }
 
    if (checkForSave)
    {
        ar.Handled = true;
        selectedTab = tabControl.SelectedIndex;
        tabControl.SelectedIndex = currentTab;
        //This method does the confirmation, sets checkForSave = false and calls the changeTab method.
        CheckSave(changeTab);
    }
    else
    {
        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 Answers, 1 is accepted

Sort by
0
Rich
Top achievements
Rank 1
answered on 03 Nov 2013, 04:38 AM
Any help on this from the Telerik folks???
0
Tina Stancheva
Telerik team
answered on 04 Nov 2013, 06:47 PM
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.

Regards,
Tina Stancheva
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.
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 >>
Tags
TabControl
Asked by
Rich
Top achievements
Rank 1
Answers by
Rich
Top achievements
Rank 1
Tina Stancheva
Telerik team
Share this question
or