by Keyvan Nayyeri via Keyvan Nayyeri on 1/21/2007 3:18:45 PM
Using tab controls wasn't so hard in Windows Forms applications. But in Windows Presentation Foundation it's easier. Thank to XAML, you can build a tab control from the scratch with markup codes.
Two elements play main roles in building a tab control: TabControl and TabItem. TabControl is the container of one or more TabItem elements.
In addition to some attributes that it inherits form its parent, TabControl has a specific attribute and an event handler. TabStripPlacement is an attribute (property) that lets you to change the position of tab strip (default is Top). SelectionChanged is an event handler that fires when user changes the selection of tab items.
Each TabControl can contain a collection of TabItem elements. TabItem has two specific attributes. Header is the string value that you see on top of each tab and IsSelected is a Boolean value that specifies if a tab is selected. Apparently only one tab can be selected at a time otherwise the first tab in list will be selected.
Below is an example of a TabControl with three TabItems.
<Window x:Class="TabControlSample.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Tab Control" Height="275" Width="300"
>
<StackPanel>
<TabControl TabStripPlacement="Top">
<TabItem Header="Tab1" IsSelected="True">
<TextBlock>
<Bold>Keyvan</Bold>
</TextBlock>
</TabItem>
<TabItem Header="Tab2">
<Image Source="Image.jpg" Stretch="UniformToFill" />
<TabItem Header="Tab3">
:-D
</TabControl>
</StackPanel>
</Window>
In second example I change tab strip position and default selection.
Title="Tab Control" Height="225" Width="300"
<TabControl TabStripPlacement="Left">
<TabItem Header="Tab1">
<TabItem Header="Tab2" IsSelected="True">
It's also possible to do things via programming codes. Below I create a TabControl with two tabs and simple text content on fly.
public Window1()
{
InitializeComponent();
TabItem item1 = new TabItem();
item1.Header = "Tab1";
item1.Content = "This is Tab1.";
TabItem item2 = new TabItem();
item2.Header = "Tab2";
item2.Content = "This is Tab2.";
TabControl tabControl = new TabControl();
tabControl.Items.Add(item1);
tabControl.Items.Add(item2);
StackPanel stackPanel = new StackPanel();
stackPanel.Children.Add(tabControl);
this.Content = stackPanel;
}
That's it!
Original Post: Tab Control in Windows Presentation Foundation
The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.