Skip to content

Commit 8da1acc

Browse files
authored
Update how-to-bind-tabs.md (#661)
* Update how-to-bind-tabs.md * Update how-to-bind-tabs.md
1 parent 7273e41 commit 8da1acc

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

docs/guides/data-binding/how-to-bind-tabs.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,42 @@ title: How To Bind Tabs
66

77
## Binding Support Example
88

9-
You can dynamically create tab items with **data binding**. To do this, bind the `ItemsSource` property of a tab control to an array of objects representing the tab header and content.
9+
You can dynamically create tab items with **data binding**. To do this, bind the `ItemsSource` property of a tab control to a collection of objects representing the tab header and content.
1010

1111
You can then use a **data template** to display the objects.
1212

13-
This example uses an array of objects created from this `TabItemViewModel` class:
13+
This example uses a collection of objects created from this `ItemViewModel` class:
1414

1515
```csharp
1616
namespace MyApp.ViewModel;
1717

18-
public class TabItemViewModel
18+
public class ItemViewModel
1919
{
2020
public string Header { get; }
2121
public string Content { get; }
22-
public TabItemViewModel(string header, string content)
22+
public ItemViewModel(string header, string content)
2323
{
2424
Header = header;
2525
Content = content;
2626
}
2727
}
2828
```
2929

30-
Create an array of two `TabItemViewModel` instances and bind it to the DataContext.
30+
Create a property that accesses a collection of `ItemViewModel` instances.
3131

3232
```csharp
33-
DataContext = new TabItemViewModel[] {
34-
new TabItemViewModel("One", "Some content on first tab"),
35-
new TabItemViewModel("Two", "Some content on second tab"),
33+
public ObservableCollection<ItemViewModel> Items { get; set; } = new() {
34+
new ItemViewModel("One", "Some content on first tab"),
35+
new ItemViewModel("Two", "Some content on second tab"),
3636
};
3737
```
3838

3939
The `TabStrip` header content is defined by ItemTemplate property, while `TabItem`'s content is defined by ContentTemplate property.
4040

41-
Finally create a `TabControl` and bind its `ItemsSource` property to the DataContext.
41+
Finally create a `TabControl` and bind its `ItemsSource` property to the `Items`.
4242

4343
```xml
44-
<TabControl ItemsSource="{Binding}">
44+
<TabControl ItemsSource="{Binding Items}">
4545
<TabControl.ItemTemplate>
4646
<DataTemplate>
4747
<TextBlock Text="{Binding Header}" />
@@ -54,7 +54,7 @@ Finally create a `TabControl` and bind its `ItemsSource` property to the DataCon
5454
xmlns:vm="using:MyApp.ViewModel"
5555
or
5656
xmlns:vm="clr-namespace:MyApp.ViewModel;assembly=MyApp.ViewModel" -->
57-
<DataTemplate DataType="vm:TabItemViewModel">
57+
<DataTemplate DataType="vm:ItemViewModel">
5858
<DockPanel LastChildFill="True">
5959
<TextBlock Text="This is content of selected tab" DockPanel.Dock="Top" FontWeight="Bold" />
6060
<TextBlock Text="{Binding Content}" />

0 commit comments

Comments
 (0)