标签控件可见性隐藏

时间:2011-02-22 06:06:36

标签: wpf tabcontrol

我有3个标签项要实现。当程序执行时,我希望用户只能看到tab1并隐藏选项卡2和选项卡3.

当程序执行时会发生类似的事情:

public Window1()
{   
    InitializeComponent();

    // I need tabs 2 and 3 to be hidden   
}

然后我在标签1中有一个按钮。当用户点击它时,标签2显示但仍然隐藏标签3

private void Button1_Click(object sender, RoutedEventArgs e)
{
    tabcontrol1.SelectedIndex = 1;
    //need some code to show tab 2
}

我在标签2中有一个按钮,显示标签3,然后所有标签都可见

private void Button2_Click(object sender, RoutedEventArgs e)
{
    tabcontrol1.SelectedIndex = 2;
    // need some code to show tab 3    
}

我的XAML代码:

<TabControl Name="Tabcontrol1" Margin=" 5" SelectedIndex="0">
    <TabItem Header="Directories">
        <Grid Width="1185" Height="945" Background="White" >
            <Label Height="28" HorizontalAlignment="Right" 
                    Margin="0,0,25,0" Name="label11" VerticalAlignment="Top"  
                    Width="120">Step 1 of 2</Label>
        </Grid>
    </TabItem>
    <TabItem Header="Properties" Opacity="1" Name="Properties">
        <Grid Width="1185" Height="945" Background="White" >
            <Button Height="32" Name="Button1" VerticalAlignment="Bottom" 
                    HorizontalAlignment="Right" Width="82" Click="Button1_Click" 
                    Margin="0,0,41,49">Build</Button>
        </Grid>
    </TabItem>
    <TabItem Header ="Output">
        <Grid Width="1185" Height="945" Background="White">
            <Button Height="32" Name="Button2" VerticalAlignment="Bottom" 
                    HorizontalAlignment="Right" Width="82" Click="Button2_Click" 
                    Margin="0,0,41,49">Build</Button>
        </Grid>
     </TabItem>
</TabControl>

我很困惑,因为我只能选择一个标签:

tabcontrol1.SelectedIndex = 1;

我正在考虑实施

tabcontrol1.SelectedIndex.Visibility = Hidden;

请建议谢谢。

2 个答案:

答案 0 :(得分:6)

最初将第二个和第三个标签的Visibility设置为Collapsed。并且还给他们一个名称,以便能够在后面的代码中访问它们。

<TabItem Name="TabItem2" Header="Properties" Opacity="1" Name="Properties" Visibility="Collapsed">
...
<TabItem Name="TabItem3" Header ="Output" Visibility="Collapsed">
...

将按钮点击代码更改为以下内容:

private void Button1_Click(object sender, RoutedEventArgs e)
{
    tabcontrol1.SelectedIndex = 1;
    //need some code to show tab 2
    TabItem2.Visibility = Visibility.Visible;
}

private void Button2_Click(object sender, RoutedEventArgs e)
{
    tabcontrol1.SelectedIndex = 2;
    // need some code to show tab 3 
    TabItem3.Visibility = Visibility.Visible;   
}

答案 1 :(得分:1)

我没有正确理解你的问题是什么。基于我的理解你想在tabcontrol中动态显示tabitems。如果以下代码有效,请检查

<Grid x:Name="container">
        <TabControl Name="Tabcontrol1" Margin=" 5" SelectedIndex="0">
            <TabItem Name="Tab1" Header="Directories">
                <Button Height="32" Name="Button1" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="82" Click="Button1_Click" Margin="0,0,41,49">Build</Button>
            </TabItem>
            <TabItem Header="Properties" Opacity="1" Name="Properties" Visibility="Collapsed">
                    <Button Height="32" Name="Button2" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="82" Click="Button2_Click" Margin="0,0,41,49">Build</Button>
            </TabItem>
            <TabItem Header="Output" Name="Tab3" Visibility="Collapsed">
                    <Button Height="32" Name="Button3" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="82" Click="Button3_Click" Margin="0,0,41,49">Build</Button>
            </TabItem>
        </TabControl>


    </Grid>

和代码隐藏

 private void Button1_Click(object sender, RoutedEventArgs e)
        {
            Properties.Visibility = Visibility.Visible;
            Tabcontrol1.SelectedItem = Properties;
        }

        private void Button2_Click(object sender, RoutedEventArgs e)
        {
            Tab3.Visibility = Visibility.Visible;
            Tabcontrol1.SelectedItem = Tab3;
        }