tab control visibility hidden
I have 3 tab items to implement. When the program executes, I would like user to only be able to see tab1 and hide tab 2 and tab 3.
something like this happens when the program execute:
public Window1()
{
InitializeComponent();
// I need tabs 2 and 3 to be hidden
}
Then I have a button in tab 1. When users click this, tab 2 shows up but still hide tab 3
private void Button1_Click(object sender, RoutedEventArgs e)
{
tabcontrol1.SelectedIndex = 1;
//need some code to show tab 2
}
And I have a button in tab 2 to show tab 3 and then all the tabs are visible
private void Button2_Click(object sender, RoutedEventArgs e)
{
tabcontrol1.SelectedIndex = 2;
// need some code to show tab 3
}
My XAML code:
<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"
Widt开发者_运维百科h="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>
I am quite confused because i can only select a tab using:
tabcontrol1.SelectedIndex = 1;
I was thinking along the line of implementing
tabcontrol1.SelectedIndex.Visibility = Hidden;
please advice thanks.
Set Visibility
of 2nd and 3rd tab to Collapsed
initially. And also give them a name to be able to access them in code behind.
<TabItem Name="TabItem2" Header="Properties" Opacity="1" Name="Properties" Visibility="Collapsed">
...
<TabItem Name="TabItem3" Header ="Output" Visibility="Collapsed">
...
And change your button click code to the following:
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;
}
I didn't understand properly what your problem is.Based on my Understanding you want to dynamically show tabitems inside a tabcontrol.Check if the below code works
<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>
and in codebehind
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;
}
精彩评论