Try this. But this actually uses treeview entirely. Because when you use combobox inside of the combobox it's hard to detect whether you are changing the travelNode or or you are changing person when you drop down your combobox.
MainViewModel.cs:
public class MainViewModel
{
public List<People> PeopleList { get; set; }
public MainViewModel() { GenerateList(); }
public void GenerateList()
{
PeopleList = new List<People>();
TravelNode car = new TravelNode { VehicleName = "Car" };
TravelNode bus = new TravelNode { VehicleName = "Bus" };
TravelNode bike = new TravelNode { VehicleName = "Bike" };
TravelNode train = new TravelNode { VehicleName = "Train" };
People john = new People();
john.Name = "John";
john.TravelNodes = new List<TravelNode>();
john.TravelNodes.Add(car); john.TravelNodes.Add(bike);
People jerry=new People();
jerry.Name="Jerry";
jerry.TravelNodes=new List<TravelNode>();
jerry.TravelNodes.Add(bus); jerry.TravelNodes.Add(train);
PeopleList.Add(john); PeopleList.Add(jerry);
}
}
public class People
{
public List<TravelNode> TravelNodes { get; set; }
public string Name { get; set; }
}
public class TravelNode
{
public string VehicleName { get; set; }
}
MainWindow.xaml.cs
public partial class MainWindow : Window
{
MainViewModel viewmodel = new MainViewModel();
public MainWindow()
{
InitializeComponent();
this.DataContext = viewmodel;
}
}
MainWindow.xaml
<Window x:Class="treeViewTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TreeView ItemsSource="{Binding PeopleList}">
<TreeView.ItemTemplate>
<DataTemplate>
<TreeViewItem ItemsSource="{Binding TravelNodes}" Header="{Binding Name}">
<TreeViewItem.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding VehicleName}"></TextBlock>
</StackPanel>
</DataTemplate>
</TreeViewItem.ItemTemplate>
</TreeViewItem>
</DataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</Grid>
If selecting is not really important, but you want just combobox inside of the combobox to display people and travelNodes, than you will change the MainWindow.xaml as following
<Window x:Class="treeViewTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<ComboBox ItemsSource="{Binding PeopleList}" SelectedIndex="0">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Name}">
</TextBlock>
<ComboBox ItemsSource="{Binding TravelNodes}" >
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding VehicleName}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>