简体   繁体   中英

Tab control selecting first tab by default

I am not sure if this is just the default WPF tab control behaviour or if there is a way to disable it.

I have a tab control defined as 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 example:

Foo
Bar
Baz

Initially, SelectedProduct is null but when the tab control is displayed, it has automatically selected Foo . What I want is for no tab to be selected at all.

Will the tab control always select the first tab?

UPDATE

I 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 code above, the app starts with "TAB 2" displayed and none of the tabs Foo/Bar/Baz are selected. However, if you change

<TabControl SelectedIndex="1">

to

<TabControl SelectedIndex="0">

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

I don't understand why if you start on "TAB 2" it works as I expect but if you start on "TAB 1" and then switch to "TAB 2" a tab gets selected by default.

I just had the same issue. I Know this is an old question, but I thought I'd leave the answer here for future me.

Use the loaded even to via in the xaml:

<TabControl Loaded="SheetTabs_Loaded"></TabControl>

In the ".xaml.cs" create the method:

private void SheetTabs_Loaded(object sender, RoutedEventArgs e)
{
    var tabControl = (TabControl)sender;
    tabControl.SelectedIndex = 0;
}

As of NET5.0 it seems to work with

<TabControl SelectedIndex="0">
...
</TabControl>

but it didn't work when I also used SelectedItem. Just with "blank" SelectedIndex, it worked.

A tab control always displays the content of one of the tabs, so if you don't tell it explicitly which one, it will use the first one. You can not have it display no tab at all.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM