TabControl - preventing the user from changing the selected tab: MessageBox that causes an error

I walked away a bit from this problem and found part of the solution.

I am trying to customize TabControl so that in some cases I might not change the user tab. When the user is unable to change the currently selected tab, a dialog box is displayed.

I have already read the following documents:

I implemented the solution indicated in the 3rd link (although all of the above creates the same error as below). And it works , but ...

Things are messy if the user does the following:

  • tries to change the tab when such an action is prohibited. MessageBox appears with an error.
  • the user clicks "OK" and returns to the original window.
  • the user again tries to change the tab. No MessageBox.
  • if the user minimizes the window and then maximizes it again, a MessageBox message should appear that should have appeared earlier.
  • the user clicks “OK” and returns to the original window ... but the tab has been changed to the one they selected before, even if they cannot change the tabs .

, , . MessageBox , ?

MessageBox, .

TabControl.SelectionChanged:

bool _isChanging = false;

    private void tabControlForNavigation_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (!_isChanging && canChangeTabs.IsChecked.HasValue)
        {
            _isChanging = true;


            bool canLeave = canChangeTabs.IsChecked.Value;  //normally this would be replaced by a check in the ViewModel

            if (!canLeave)
            {
                int prevIndex = tabControlForNavigation.Items.IndexOf(tabControlForNavigation.SelectedContent);
                tabControlForNavigation.SelectedIndex = prevIndex;
                MessageBox.Show("Can't change tabs!"); //if I comment out this line, everything works fine.
            }

            _isChanging = false;
        }
    }

MVVM . :

<Window x:Class="TestTabControlSwitching.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow"
    Height="350"
    Width="525">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition />
    </Grid.RowDefinitions>
    <CheckBox x:Name="canChangeTabs"
              Content="Can Change Tabs"
              IsChecked="True" />
    <TabControl x:Name="tabControlForNavigation"
                Grid.Row="1"
                IsSynchronizedWithCurrentItem="True"
                ItemsSource="{Binding Collection}"
                SelectedItem="{Binding SelectedItem}"
                SelectionChanged="tabControlForNavigation_SelectionChanged"
                Margin="4"
                HorizontalAlignment="Stretch">
        <TabControl.ItemTemplate>
            <DataTemplate>
                <ContentPresenter Content="{Binding Path=Name}" />
            </DataTemplate>
        </TabControl.ItemTemplate>

    </TabControl>
</Grid>

- ViewModel, .

+5
4

, MessageBox . MessageBox, . .

SO
WPF: MessageBox PreviewMouseDown?
Wpf MessageBox?

, Window, MessageBox, Show ( ShowDialog) .

+5

, , :

tab_Enter , , MessageBox , myTabs.SelectedIndex . :

    private void someTab_Enter(object sender, EventArgs e)
    {
        if (myCondition)
        {
            MessageBox.Show("Sorry, myCondition will not let you move to this tab.");
            myTabs.SelectedIndex = someOtherTabIndex;
        }
    }
0

. , (.. , ), Google MSDN.

[TabControl SelectionChanged ?]

Please ignore the poorly formatted question and answer. But, as mentioned in the answer, inserting it into the dispatcher and focusing the selected tab after setting the index, solve the problem for me.

0
source

You are missing a trick. Just make focusable = False for the tab title.

<TabItem Header="MY TAB" Focusable="False">

You can bind this property to your view model.

<TabItem Header="MY TAB" Focusable="{Binding Bool_CanHasCheeseBurger}">
0
source

All Articles