如何在WPF中以编程方式显示折叠的选项卡项?

时间:2015-02-10 20:20:06

标签: wpf tabs visibility tabitem

如何在WPF中以编程方式显示折叠的标签项?

我创建了一个带有tabcontrol的xaml文件,其中包含三个选项卡,我已成功设置为Visible的visibility属性。隐藏和崩溃。另外我有两个按钮,一个用于将Tab 2设置为可见,另一个用于将Tab 3设置为可见。 在后面的代码中,我为每个按钮设置了一个事件处理程序。

事件处理程序无法编译。

我将不胜感激任何建议。谢谢!

Here is my MainWindow.xaml

<Window x:Class="WPFTabItemVisibility.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">
    <StackPanel>
        <TabControl>
            <TabItem Header="Tab 1" Visibility="Visible" Content="Tab 1 Content goes here"></TabItem>
            <TabItem Header="Tab 2" Visibility="Hidden" Content="Tab 2 Content goes here"></TabItem>
            <TabItem Header="Tab 3" Visibility="Collapsed" Content="Tab 3 Content goes here"></TabItem>
        </TabControl>

        <Button Content="Make Tab 2 Visible" Margin="20" Height="30" Width="200" Click="Button_Click"/>
        <Button Content="Make Tab 3 Visible" Margin="20" Height="30" Width="200" Click="Button_Click_1"/>
    </StackPanel>
</Window>

这是我的MainWindow.xaml.cs

 using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;    


namespace WPFTabItemVisibility
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }

            private void Button_Click(object sender, RoutedEventArgs e)
            {
                TabIndex = 1;
                TabItem.VisibilityProperty = IsVisible;

            }

            private void Button_Click_1(object sender, RoutedEventArgs e)
            {
                TabIndex = 2;
                TabItem.VisibilityProperty = IsVisible;
            }
        }
    }

Thank you!        

2 个答案:

答案 0 :(得分:2)

您可以为tabitem命名并设置其可见性。请参阅以下代码。

 <TabControl>
        <TabItem x:Name="tab1" Header="Tab 1" Visibility="Visible" Content="Tab 1 Content goes here"></TabItem>
        <TabItem x:Name="tab2" Header="Tab 2" Visibility="Hidden" Content="Tab 2 Content goes here"></TabItem>
        <TabItem x:Name="tab3" Header="Tab 3" Visibility="Collapsed" Content="Tab 3 Content goes here"></TabItem>
    </TabControl>
  private void Button_Click(object sender, RoutedEventArgs e)
    {
        tab2.Visibility = Visibility.Visible;
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        tab3.Visibility = Visibility.Visible;
    } 

答案 1 :(得分:-1)

在你的XAML中尝试这个

 <StackPanel>
    <TabControl>
        <TabItem Header="Tab 1"
                 Visibility="Visible"
                 Name="tab1"
                 Content="Tab 1 Content goes here"></TabItem>
        <TabItem Header="Tab 2"
                 Visibility="Hidden"
                 Name="tab2"
                 Content="Tab 2 Content goes here"></TabItem>
        <TabItem Header="Tab 3"
                 Name="tab3"
                 Visibility="Collapsed"
                 Content="Tab 3 Content goes here"></TabItem>
    </TabControl>

    <Button Content="Make Tab 2 Visible"
            Margin="20"
            Height="30"
            Width="200"
            Click="Button_Click" />
    <Button Content="Make Tab 3 Visible"
            Margin="20"
            Height="30"
            Width="200"
            Click="Button_Click_1" />
</StackPanel>

CodeBehind .cs文件

 private void Button_Click(object sender, RoutedEventArgs e)
    {
        //TabIndex = 1;
        //TabItem.VisibilityProperty = IsVisible;
        tab2.Visibility = Visibility.Visible;

    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        //TabIndex = 2;
        //TabItem.VisibilityProperty = IsVisible;
        tab3.Visibility = Visibility.Visible;
    }