Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tab control selecting first tab by default

Tags:

c#

wpf

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.

like image 311
Flack Avatar asked Dec 03 '13 16:12

Flack


2 Answers

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.

like image 159
Andreas Busslinger Avatar answered Nov 14 '22 15:11

Andreas Busslinger


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 event via 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;
}
like image 3
Monofuse Avatar answered Nov 14 '22 13:11

Monofuse