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;
if (!canLeave)
{
int prevIndex = tabControlForNavigation.Items.IndexOf(tabControlForNavigation.SelectedContent);
tabControlForNavigation.SelectedIndex = prevIndex;
MessageBox.Show("Can't change tabs!");
}
_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, .