Reputation: 29693
I want to use dataTemplale inside dataTemplate. I want to display data in list box like that:
<RoomName>
<Kid Name>
<Kid Name>
<RoomName>
<Kid Name>
<RoomName>
<Kid Name>
<Kid Name>
<Kid Name>
This is what I got. It's not working.
class Room
{
ObservableCollection<kid> Kids = new Ob...
}
class School
{
ObservableCollection<Room> Rooms = new Ob...
}
class kid
{
Name;
Size;
(...)
}
This is my binding from code:
School BigSchool = new School();
MainListBox.DataContext = BigSchool;
This is my Window XAML:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Rapideo_Client" mc:Ignorable="d"
x:Class="Rapideo_Client.MainWindow"
Title="Client" SnapsToDevicePixels="True" Height="400" Width="625">
<Window.Resources>
<DataTemplate x:Key="kidTemplate" DataType="Kid">
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Path=Name}" FontWeight="Bold"></TextBlock>
<Label Content="{Binding Path=Size}"/>
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="roomTemplate" DataType="Room">
<StackPanel Orientation="Vertical">
<Label Content="{Binding Path=Kids.Count}"/>
<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled" ItemsSource="{Binding Path=Kids}" ItemTemplate="{DynamicResource kidTemplate}" />
</StackPanel>
</DataTemplate>
</Window.Resources>
<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Visible" x:Name="MainListBox" Grid.Column="1" ItemsSource="{Binding Rooms}" ItemTemplate="{DynamicResource roomTemplate}" />
</Window>
Upvotes: 2
Views: 2981
Reputation: 11230
Why not just use a ListView with grouping? Then you could use a single ListView with one data template for items and one for the group. Just set a GroupStyle on the ListView and it will do exactly what you want.
An example:
<DataTemplate x:Uid="DataTemplate_4" x:Key="MetadataGroupDisplayTemplate">
<TextBlock x:Uid="TextBlock_4" Text="{Binding Path=RoomName}" FontWeight="Bold" Margin="0,5,0,0"/>
</DataTemplate>
GroupStyle gs = new GroupStyle();
gs.HeaderTemplate = FindResource("MetadataGroupDisplayTemplate") as DataTemplate;
MyListView.GroupStyle.Add(gs);
Upvotes: 2
Reputation: 185410
Data Binding (read this!) only works with public properties, in your code i only see fields.
Also if that is not it, check the Output window of Visual Studio for binding errors, also check this site for more info on databinding debugging.
Upvotes: 1