Search code examples
c#.netxamlitemsource

Why do we use itemsource actually?


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?


Solution

  • 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>