Adding TabItems dynamically

倾然丶 夕夏残阳落幕 提交于 2019-11-30 05:14:01
H.B.

You set the wrong thing, you should not modify the ItemContainerStyle but the TabControl.ItemTemplate for the header and TabControl.ContentTemplate for the content.

(The exception may have to do with the fact that in the style only one VillageUserControl is created, but the style applies to multiple tab items.)

Sami Abdelgadir Mohammed

Now it is working:

<TabControl Name="Farms_myVillages" 
           ItemsSource="{Binding Villages}">
       <TabControl.ItemTemplate>
            <DataTemplate>
                 <TextBlock Text="{Binding Name}"/>
            </DataTemplate>
       </TabControl.ItemTemplate>
      <TabControl.ContentTemplate>
            <DataTemplate>
                <u:VillageResources/>
            </DataTemplate>
      </TabControl.ContentTemplate>
</TabControl>

Your approach of not having this in code behind is right, instead of using ItemContainerStyle use ItemTemplate and ContentTemplate. You can have a look at this sample from Josh Smith for creating a tabs using Templates and Styles -

http://code.msdn.microsoft.com/mag200902MVVM/Release/ProjectReleases.aspx?ReleaseId=2026

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!