Tab management when selecting the first tab

I'm not sure if this is just the default WPF tab management behavior or if there is a way to disable it.

I have a tab control that is defined below:

<TabControl TabStripPlacement="Left" Background="Transparent" ItemsSource="{Binding Path=AvailableProducts}" SelectedValuePath="Name" SelectedValue="{Binding Path=SelectedProduct, Mode=TwoWay}"> 

AvailableProducts is a list of products. For instance:

 Foo Bar Baz 

SelectedProduct is initially null , but when the tab control is displayed, it automatically selects Foo . I want no tab to be selected at all.

Will the tab control always select the first tab?

UPDATE

I have added some sample code that shows what I am describing.

 <Window x:Class="WpfApplication1.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> <TabControl SelectedIndex="1"> <TabItem Header="TAB 1"> <Button>TEST</Button> </TabItem> <TabItem Header="TAB 2"> <TabControl TabStripPlacement="Left" Background="Transparent" ItemsSource="{Binding Path=AvailableProducts}" SelectedValuePath="Name" SelectedValue="{Binding Path=SelectedProduct, Mode=TwoWay}"/> </TabItem> </TabControl> </Grid> </Window> using System.Collections.Generic; namespace WpfApplication1 { public partial class MainWindow { private List<Product> _availableProducts = new List<Product>(); public MainWindow() { SelectedProduct = null; InitializeComponent(); _availableProducts.Add(new Product("Foo")); _availableProducts.Add(new Product("Bar")); _availableProducts.Add(new Product("Baz")); DataContext = this; } public List<Product> AvailableProducts { get { return _availableProducts; } } public string SelectedProduct { get; set; } } public class Product { public Product(string name) { Name = name; } public string Name { get; set; } public override string ToString() { return Name; } } } 

If you run the above code, the application will start showing up with β€œTAB 2” and none of the Foo / Bar / Baz tabs will be selected. However, if you change

 <TabControl SelectedIndex="1"> 

to

 <TabControl SelectedIndex="0"> 

and run the application, it starts with "TAB 1", and when you switch to "TAB 2", the first tab (Foo) is selected.

I don’t understand why, if you start β€œTAB 2”, it works as I expect, but if you start β€œTAB 1” and then switch to β€œTAB 2”, the tab is selected by default.

+9
source share
1 answer

The tab control always displays the contents of one of the tabs, so if you do not explicitly specify which one, it will use the first. You are not able to display the tab.

You can try to hide / disable the tab / tab control until a product is selected.

0
source

Source: https://habr.com/ru/post/959254/


All Articles