wpf 选项卡控件默认选择第一个选项卡

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20356239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:09:22  来源:igfitidea点击:

Tab control selecting first tab by default

c#wpf

提问by Flack

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

我不确定这是否只是默认的 WPF 选项卡控件行为,或者是否有办法禁用它。

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 nullbut when the tab control is displayed, it has automatically selected Foo. What I want is for no tab to be selected at all.

最初, SelectedProduct 为null但当显示选项卡控件时,它已自动选择Foo。我想要的是根本不选择任何选项卡。

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

如果您运行上面的代码,应用程序以显示“TAB 2”开始,并且没有选择任何选项卡 Foo/Bar/Baz。然而,如果你改变

<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).

并运行应用程序,它从“TAB 1”开始,当您切换到“TAB 2”时,第一个选项卡被选中(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.

我不明白为什么如果你从“TAB 2”开始它会按我的预期工作,但如果你从“TAB 1”开始然后切换到“TAB 2”,默认情况下会选择一个选项卡。

回答by Thorsten Dittmar

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.

您可以尝试隐藏/禁用选项卡/选项卡控件,直到选择了产品。