I have some TabItem controls in a WPF project and I need to block them from opening when a certain event is happening. Basically I set a Boolean variable to true when the process occurs, and false when it ends and as long as the property is true, no other TabItem can be opened. My problem is why I haven't found the method that controls the opening of the TabItem. Can anyone give me a help on how to solve this problem?
You could create a custom TabControl
and override the OnSelectionChanged
method:
public class CustomTabControl :TabControl
{
private bool _handle = true;
public bool IsSelectable { get; set; } = true;
protected override void OnSelectionChanged(SelectionChangedEventArgs e)
{
if(IsSelectable)
{
base.OnSelectionChanged(e);
}
else if (_handle && e.RemovedItems.Count > 0)
{
_handle = false;
SelectedItem = e.RemovedItems[0];
_handle = true;
}
}
}
Set IsSelectable
to false
to prevent tabs from being selected:
<local:CustomTabControl IsSelectable="False">
<TabItem Header="1">
<TextBox Text="..." />
</TabItem>
<TabItem Header="2" >
<TextBox Text="..." />
</TabItem>
<TabItem Header="3">
<TextBox Text="..." />
</TabItem>
</local:CustomTabControl>