Geeks With Blogs
Nat Luengnaruemitchai Geek Blog

I posted in MSDN Forum a while ago that I ran into a WPF bug that occurs when you put one template inside another template like

    <ListBox HorizontalContentAlignment="Stretch" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
        Background="Honeydew" ItemsSource="{Binding}">
        <ListBox.ItemTemplate>
                <DataTemplate>
   <ListBox ItemsSource="{Binding}">
    <ListBox.ItemTemplate>
     <DataTemplate>
      <etc..............
     </DataTemplate>
    </ListBox.ItemTemplate>
   </ListBox>
  </DataTemplate>
 </ListBox.ItemTemplate>
    </ListBox>

The above code won't fail during the compilation. However,during the run, you will notice that only the first item of the outer listbox will show up but not the rest. Sam Bent, WPF/WinFX Dev Lead answered that this is a nested template bug. If template is used nestedly, the result will be unexpected. To workaround, the template should be defined as a separated template in Resources section like:

<Window.Resources>
    <DataTemplate x:Key="InnerTemplate">
        <StackPanel>
            <ComboBox ItemsSource="{Binding Source={StaticResource CategoryData}, XPath=Category}"
            SelectedValue="{Binding XPath=Category}"/>
            <TextBlock Text="{Binding XPath=Title}" />
            <TextBlock Text="{Binding XPath=Summary}" />
        </StackPanel>
    </DataTemplate>
    <DataTemplate x:Key="OuterTemplate">
        <StackPanel>
            <ListBox HorizontalContentAlignment="Stretch" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
                ItemTemplate="{StaticResource InnerTemplate}" Background="Honeydew" ItemsSource="{Binding XPath=.}">
            </ListBox>
        </StackPanel>
    </DataTemplate>
</Window.Resources> 

<Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
    <ListBox HorizontalContentAlignment="Stretch" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
            ItemTemplate="{StaticResource OuterTemplate}" Background="Honeydew">
        <ListBox.ItemsSource>
            <Binding Source="{StaticResource InventoryData}"
                    XPath="*[@Stock='out'] | *[@Number>=8 or @Number=3]"/>
        </ListBox.ItemsSource>
    </ListBox>
</Grid>

 

Posted on Sunday, July 29, 2007 8:17 AM WPF | Back to top


Comments on this post: WPF Nested Template Bug

# re: WPF Nested Template Bug
Requesting Gravatar...
Thanks for the post. I had the same problem and could not find a way to fix it. Your post helped me alot.
Left by mp on Oct 11, 2008 1:48 PM

# re: WPF Nested Template Bug
Requesting Gravatar...
What was the symptom of this bug?

Left by Elan Hasson on Feb 10, 2009 3:17 AM

# re: WPF Nested Template Bug
Requesting Gravatar...
Dude I have the same problem but what you suggested dosent work.

have anymore ideas?
Left by Dan on Oct 01, 2009 10:41 AM

Your comment:
 (will show your gravatar)


Copyright © Nat Luengnaruemitchai | Powered by: GeeksWithBlogs.net