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

Actual SelectionChanging analog?

3 Answers 53 Views
TabControl
This is a migrated thread and some comments may be shown as answers.
Adam
Top achievements
Rank 1
Adam asked on 09 Nov 2012, 04:20 PM

Hi everyone,

I'm attempting to port some logic on tab change that will do a pretty standard check for dirty state, successive save, etc.  I've attempted to do something with the PreviewSelectionChanged but this seems problematic.  At the point when this event fires it is already assumed that the newly selected tab is the currently selected tab which interferes with the logic I'm trying to execute.  What I really need is a chance to register the fact that the selection is being requested but to capture the relevant data, cancel the operation, then substitute our own logic.  The other complication here is that since the overall save logic is bound to be asynchronous, there's no real way to keep the tabcontrol waiting on my input as to whether it should change tabs, hence the desire to just cancel the action altogether then be able to change tabs later manually.

Any hacks/suggestions here?

Thanks!
Adam.g

3 Answers, 1 is accepted

Sort by
0
Tina Stancheva
Telerik team
answered on 13 Nov 2012, 04:49 PM
Hello Adam,

The PreviewSelectionChanged event is the place where you can revert the selection, but you correctly pointed out that it executes after the selection is internally changed. However this behavior is by design - the RadTabControl inner selector changes the selection internally and asynchronously and if you try to customize the selection during these processes are running, you'll get undesired results. This is why the PreviewSelectionChanged event fires after the RadTabControl inner selection logic is over and it is the safest place to implement your customizations. And if you handle it, the selection will be reverted and you can implement your own logic instead.

However, as I might be missing something from your scenario, you can send us a sample solution demonstrating your approach and we'll take a closer look at it and let you know if there are other approaches that you can try.

All the best,
Tina Stancheva
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Adam
Top achievements
Rank 1
answered on 21 Nov 2012, 01:51 PM
Hi there,

Thanks for the explanation.  I think the crux of my issue is that I definitely do need to be able to tie into some logic or event that is thrown because of that tab change without the tab actually changing; a true Pre event that can be cancelled.  Do you have any ideas of how to achieve this?  I plan on playing around with extending the RadTabControl to see what can be achieved in a protected sense.

I'll keep you posted,

Adam.g
0
Adam
Top achievements
Rank 1
answered on 21 Nov 2012, 07:24 PM
Hey all,

So far, I've come up with this.  This code is a set of overrides that go in a custom class that extends the RadTabItem that we use throughout the application.

protected override async void OnHeaderMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    if (this != [Parent RadTabControl reference].SelectedItem)
    {
        int returnCode = await [Asynchronous save window logic, returns different value depending on state];

      // If the return says that we should go ahead and make the selection 
        if (returnCode > -1)
        {
            e.Handled = false;
            base.OnHeaderMouseLeftButtonDown(sender, e);
        }
    }
}
 
protected override async void OnKeyDown(System.Windows.Input.KeyEventArgs e)
{
    if (e.Key == System.Windows.Input.Key.Left || e.Key == System.Windows.Input.Key.Right)
    {
        int currentIndex = [Parent RadTabControl reference].Items.IndexOf(this);
 
        if (e.Key == System.Windows.Input.Key.Left
            && currentIndex > 0)
        {
            int returnCode = await [Asynchronous save window logic, returns different value depending on state];
 
                                 // If the return says that we should go ahead and make the selection 
            if (returnCode > -1)
            {
                ([Parent RadTabControl reference].Items[currentIndex - 1] as [Custom RadTabItemClass).IsSelected = true;
            }
        }
 
        if (e.Key == System.Windows.Input.Key.Right
            && currentIndex < [Parent RadTabControl reference].Items.Count - 1)
        {
            int returnCode = await [Asynchronous save window logic, returns different value depending on state];
 
                                 // If the return says that we should go ahead and make the selection 
            if (returnCode > -1)
            {
[Parent RadTabControl reference].Items[currentIndex + 1] as [Custom RadTabItemClass]).IsSelected = true;
            }
        }
        return;
    }
 
    base.OnKeyDown(e);
}

This seems to be working well for me.  Certainly, it's not ideal but it's succinct enough that I don't feel like I'm really "hacking" anything per se.  If anyone has any suggestions, please let me know.

Thanks!
Adam.g
Tags
TabControl
Asked by
Adam
Top achievements
Rank 1
Answers by
Tina Stancheva
Telerik team
Adam
Top achievements
Rank 1
Share this question
or