Reputation: 147
This is what I have. A mainwindow with a combobox and a tabcontrol in it, having a viewmodel attached to its datacontext. I have a usercontrol with another viewmodel , this usercontrol contains a combobox and a datagrid.Now when I select an item from the main combobox , the viewmodel will calculate how many tabs need to be generated at runtime, and it assigns the usercontrol to each tabitem. The problem I am having is , I am unable to bind the items to be populated in the combobox of each usercontrol tab item. Below is my code
MainWindow.xaml
<Window x:Class="TsstApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:TsstApp"
WindowState="Maximized"
Title="MainWindow" Height="1333" Width="1024">
<Window.DataContext>
<vm:MainViewModel/>
</Window.DataContext>
<Grid>
<ComboBox HorizontalAlignment="Left" Margin="21,22,0,0" VerticalAlignment="Top"
Width="120" ItemsSource="{Binding Items}" DisplayMemberPath="ItemName" SelectedItem="
{Binding SelectedItem}"/>
<TabControl HorizontalAlignment="Left" Height="928" Margin="10,69,0,0"
VerticalAlignment="Top" Width="996" ItemsSource="{Binding TabData}">
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Tech}"/>
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<vm:TabItemControl/>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
</Grid>
Below is the mainviewmodel
MainViewModel.cs
public class MainViewModel : ViewModelBase
{
private ItemCollection selectedItem;
public MainViewModel()
{
List<ItemCollection> ax = new List<ItemCollection>();
ax.Add(new ItemCollection() { ItemId = 0, ItemName = "Hair" });
ax.Add(new ItemCollection() { ItemId = 1, ItemName = "Fur" });
ax.Add(new ItemCollection() { ItemId = 2, ItemName = "Tail" });
this.Items = ax;
RaisePropertyChanged("Items");
this.SelectedItem = this.Items[0];
RaisePropertyChanged("SelectedItem");
}
public IList<ItemCollection> Items { get; set; }
public ItemCollection SelectedItem
{
get { return selectedItem ;}
set {
selectedItem = value;
RaisePropertyChanged("SelectedItem");
List<TabItemData> tabs = new List<TabItemData>();
switch(selectedItem.ItemName)
{
case "Hair":
for (int i = 0; i < 5;i++)
{
tabs.Add(new TabItemData() { TabId = i, TabName = "Hair"+i.ToString() });
}
break;
case "Fur":
for (int i = 0; i < 3; i++)
{
tabs.Add(new TabItemData() { TabId = i, TabName = "Fur" + i.ToString() });
}
break;
case "Tail":
for (int i = 0; i < 7; i++)
{
tabs.Add(new TabItemData() { TabId = i, TabName = "Tail" + i.ToString() });
}
break;
}
SelectedItem.TabData = tabs;
this.TabData = tabs.Select(t => new TabItemViewModel(t)).ToList();
RaisePropertyChanged("TabData");
}
}
public IList<TabItemViewModel> TabData { get; private set; }
}
Below is my usercontrol
UserControl.xaml
<UserControl x:Class="TsstApp.TabItemControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:vm="clr-namespace:TsstApp"
mc:Ignorable="d"
d:DesignHeight="1333" d:DesignWidth="1024">
<UserControl.DataContext>
<vm:TabItemViewModel/>
</UserControl.DataContext>
<Grid>
<ComboBox HorizontalAlignment="Left" Margin="75,91,0,0" VerticalAlignment="Top"
Width="148" Height="27" ItemsSource="{Binding ReleaseItems}"
SelectedValuePath="ItemId" DisplayMemberPath="ItemName"/>
<TextBox HorizontalAlignment="Left" Height="31" Margin="75,183,0,0"
TextWrapping="Wrap" Text="{Binding TextContent}" VerticalAlignment="Top" Width="148"/>
</Grid>
</UserControl>
And this is the viewmodel
TabItemViewModel.cs
public class TabItemViewModel : ViewModelBase
{
public TabItemViewModel()
{
}
public TabItemViewModel(TabItemData t)
{
this.Tech = t.TabName;
List<TabReleases> az = new List<TabReleases>();
for(int i=0;i<10;i++)
{
az.Add(new TabReleases(){ReleaseId=i , ReleaseName=t.TabName + "-" + i.ToString()});
}
this.ReleaseItems = az;
}
public string Tech { get; set; }
public IList<TabReleases> ReleaseItems { get; set; }
}
Now , the tabitem header , is working fine , as you can see , I am binding the header in the mainwindows iteself, but the combobox of the usercontrol is not getting popualated.
Kindlyhelp.
Upvotes: 0
Views: 664
Reputation: 147
I solved it finally. I was just doing one thing wrong. I was adding datacontect in the tabitemcontrol , but it is not required , as tabcontrol in mainwindows already has itemsource , which creates instances to tabitemview model
Upvotes: 0