WPF/XAML でタブの中のタブの中に(以下略
ちょっといろいろあって WPF のテスト中。
これ↓をデータバインディングでつくってみる。
ふと冷静になって TabControl を調べてみたらどうやら ItemsControl を継承しているらしく、
「ってことは ItemsTemplate?」って思ってぐぐってみたらさくっと解決。
あんなに悩んだのはなんだったんだ。。
これでいくらでも再帰的にできる。
[View]
<TabControl ItemsSource="{Binding ContainerList}" SelectedIndex="{Binding CurrentContainerIndex}"> <TabControl.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Name}"/> </DataTemplate> </TabControl.ItemTemplate> <TabControl.ContentTemplate> <DataTemplate> <TabControl ItemsSource="{Binding ItemList}" SelectedIndex="{Binding CurrentItemIndex}"> <TabControl.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Name}"/> </DataTemplate> </TabControl.ItemTemplate> <TabControl.ContentTemplate> <DataTemplate> <Button Content="{Binding Content}"/> </DataTemplate> </TabControl.ContentTemplate> </TabControl> </DataTemplate> </TabControl.ContentTemplate> </TabControl>
[ViewModel]
public class Item { public string Name { get; set; } public string Content { get; set; } } public class ItemsContainer { public string Name { get; set; } public List<Item> ItemList { get; set; } public int CurrentItemIndex { get; set; } } #region ContainerList変更通知プロパティ private List<ItemsContainer> _ContainerList; public List<ItemsContainer> ContainerList { get { return _ContainerList; } set { if (_ContainerList == value) return; _ContainerList = value; RaisePropertyChanged("ContainerList"); } } #endregion #region CurrentContainerIndex変更通知プロパティ private int _CurrentContainerIndex; public int CurrentContainerIndex { get { return _CurrentContainerIndex; } set { if (_CurrentContainerIndex == value) return; _CurrentContainerIndex = value; RaisePropertyChanged("CurrentContainerIndex"); } } #endregion public void Initialize() { var containerList = new List<ItemsContainer>(); for (int i = 0; i < 2; ++i) { var container = new ItemsContainer() { Name = "a" + i.ToString(), ItemList = new List<Item>(), CurrentItemIndex = 0, }; for (int j = 0; j < 3; ++j) { var item = new Item() { Name = "b" + j.ToString(), Content = "item content" + j.ToString(), }; container.ItemList.Add(item); } containerList.Add(container); } this.ContainerList = containerList; }