I was recently having issues with a XAML structure I was working on. The stucture of my XAML was as follows.
LISTBOX (Bound to collection of custom objects)
{Item Template}
ListView (Bound to Collection of Child Objects that hangs of the Parent binding)
Now when I ran the app I got the binding firing for each parent and associated child collection but when the xaml renders only the first listboxitem has a listview bound below it. Is this a bug in the way ListViews work when nested within a Listbox's data template?
The answer it turns out is a big YES. Apparently, this is a know issue in WPF. In order to resolve this issue you need to make sure that all your datatemplates are moved to resources. Only then will the databinging and rendering work.
My XAML is show below for reference.
Window x:Class="WpfApplication1.Window1" xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml Title="Window1" Height="300" Width="300" >
<StackPanel VerticalAlignment="Stretch" >
<Button Click="Button_Click">Run Test</Button>
<ListBox Name="ParentListBox" VerticalAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Border BorderThickness="1" BorderBrush="Black">
<StackPanel>
<TextBlock Height="25" Text="{Binding Path=ParentName}"></TextBlock>
<ListView ItemsSource="{Binding Path=Children}" Margin="0" Name="SubList">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Width="300" Header="Child">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=ChildName}" TextWrapping="Wrap"></TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
</StackPanel>
</Border>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</StackPanel></Window>
<