0

I am new to .net and i studied on msdn that it "represents a control that can be used to present a collection of items." By this line what i understand is suppose if i use it for TabControl then it provides a control which will enable several TabItems (collection) to render on the given conatiner.

<controls:TabControl Grid.Row="0" BorderThickness="0" Background="White" 
                     ItemsSource="{Binding TabList, Mode=TwoWay, Converter={StaticResource TabConverter}}"

Could someone please correct (if i am wrong) with an example easy to understand showing why do we use it. What happen if we dont use it?

user3735822
  • 337
  • 2
  • 12
  • What outside "you do not have anything inside the control"? – TomTom Sep 04 '14 at 12:37
  • 1
    `ItemsSource` drives the collection of tabs in the control. It requires an `IEnumerable` typed object. In simple terms the `TabControl` counts the number of objects in the enumerable, spawns a tab for each object, sets the datacontext of the instantiated tab (to the enumerated object) and the binding system does the rest. – Charleh Sep 04 '14 at 12:40

1 Answers1

1

The purpose of the ItemsSource it so create a dynamic number of tabs depending on some data stored in a class (You need to set the DataContext of the Window though.

If you don't use ItemsSource, you could use separate TabItems to create a static number of tabs.

So it is this (showing a tab for each name in the list):

<TabControl ItemsSource="{Binding ListOfNames}}" />

Opposing to:

<TabControl>
  <TabItem Header="John">
  </TabItem>
  <TabItem Header="Jane">
  </TabItem>
  <TabItem Header="Dave">
  </TabItem>
</TabControl>
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • in my case i have to create number of tabItems that i dont know at start (will be created dynamically by button click) . So the only way to do is ItemSorce ? right ? – user3735822 Sep 04 '14 at 12:44
  • Or in the Window code them by hand. Using a viewmodel and binding is more work to start, but will give you much joy when maintaining it. – Patrick Hofman Sep 04 '14 at 12:45
  • sorry i couldnt understand . you mean either do statically in xaml ? (but i dont know how many Tabitem user will ad on buttonclick) – user3735822 Sep 04 '14 at 12:47
  • Eventhing you do in XAML will eventually become code. See this answer as example: http://stackoverflow.com/a/5487907/993547. – Patrick Hofman Sep 04 '14 at 12:49
  • actual problem was this (thanks if you could answer tehre also) http://stackoverflow.com/questions/25661264/how-to-update-the-data-to-anothers-view-xaml-from-another-viewmodel-class-mv – user3735822 Sep 04 '14 at 12:55