Nat Luengnaruemitchai

Geek Blog

  Home  |   Contact  |   Syndication    |   Login
  100 Posts | 0 Stories | 300 Comments | 232 Trackbacks

News

Archives

Post Categories

Blogroll

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>

 

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
posted on Sunday, July 29, 2007 8:17 AM

Feedback

# re: WPF Nested Template Bug 10/11/2008 1:48 PM mp
Thanks for the post. I had the same problem and could not find a way to fix it. Your post helped me alot.

# re: WPF Nested Template Bug 2/10/2009 3:17 AM Elan Hasson
What was the symptom of this bug?



# re: WPF Nested Template Bug 10/1/2009 10:41 AM Dan
Dude I have the same problem but what you suggested dosent work.

have anymore ideas?

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: