0

I want to lock my wpf tab to change the index but I'm getting dispatcher error messages with my code below. Where am I doing wrong? I'm aware that once the content changes, it fires the same event but is there any other event to fire for this ?

    private void MainTabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (!(e.Source is TabControl))
            return;
        if (Helper.GetProperty<bool>("IsTabLocked")) // my condition
        {
            MessageBox.Show("tab is locked");
            e.Handled = true;
            return;
        }
Kubi
  • 2,139
  • 6
  • 35
  • 62
  • Is this helpful http://stackoverflow.com/questions/418006/how-can-i-disable-a-tab-inside-a-tabcontrol – CharithJ Sep 08 '11 at 00:13
  • not really, but e.cancel = true, I think I need something like this. SelectionChangedEventArgs have any attribute like this ? – Kubi Sep 08 '11 at 00:35
  • Yes, Handled Property. But not sure whether it would work in "SelectionCHANGED" event. http://msdn.microsoft.com/en-us/library/system.windows.routedeventargs.handled.aspx – CharithJ Sep 08 '11 at 00:58
  • it does not work as in my code – Kubi Sep 09 '11 at 04:03

2 Answers2

0

The easiest solution that I can see is setting the required tab as the selected one when the execution comes to SelectionChanged event.

Try something like below.

int MyPreferedTabPageIndex = 1; // ?

private void MainTabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (Helper.GetProperty<bool>("IsTabLocked")) // my condition
        {
            MainTabControl.SelectedIndex = MyPreferedTabPageIndex ;
            MessageBox.Show("tab is locked");

        }
    }
CharithJ
  • 46,289
  • 20
  • 116
  • 131
  • the problem here is that it fires the same event more than 1 time for each change. I've added a control to prevent t other changes in selection content. How are you going to update the index ? – Kubi Sep 09 '11 at 04:19
  • and i guess there becomes an infinitive loop since once you change the index, it fires the event again and again and again, which was my exact problem. e.Handled = true; didn't work. – Kubi Sep 09 '11 at 04:26
0

I could come up with a custom solution with the source code below, but I'm sure someone has thought about this and there is an event or an easier trick which I've never heard before.

    static int TabControlIndex = 0;
    private void MainTabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (!(e.Source is TabControl))
            return;
        if (TabControlIndex == MainTabControl.SelectedIndex)
            return;
        if (Helper.GetProperty<bool>("IsTabLocked") && TabControlIndex != MainTabControl.SelectedIndex)
        {
            MessageBox.Show("locked");
            MainTabControl.SelectedIndex = TabControlIndex;
            // = true;
            return;
        }
Kubi
  • 2,139
  • 6
  • 35
  • 62