Grid with named RowDefinition fails to populate as ItemsPanel in WPF4
Asked Answered
N

1

6

I have a Grid defined in an ItemsControl ItemsPanelTemplate, and one of RowDefinitions has a x:Name defined (so I could animate the row size).

<ItemsControl ItemsSource="{Binding Data, Source={StaticResource model}}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition x:Name="t" />
                    <RowDefinition />
                </Grid.RowDefinitions>
            </Grid>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

This worked fine in 3.5, however when we recently upgraded to 4.0 it all fell apart. What I would see is a Grid with the defined Row and Column definitions but no children.

If I set IsItemsHost=true on the Grid, everything starts working. If I add an x:Name to the Grid itself, or remove the x:Name from the RowDefinition it all works.

<ItemsPanelTemplate>
    <Grid IsItemsHost="True">
        <Grid.RowDefinitions>
            <RowDefinition x:Name="t" />
            <RowDefinition />
        </Grid.RowDefinitions>
    </Grid>
</ItemsPanelTemplate>

or

<ItemsPanelTemplate>
    <Grid x:Name="g">
        <Grid.RowDefinitions>
            <RowDefinition x:Name="t" />
            <RowDefinition />
        </Grid.RowDefinitions>
    </Grid>
</ItemsPanelTemplate>

This seems like a bug, but I wanted to check with the community and see if people agreed, or if I've overlooked something. I could not find anything on Connect or the web, so can anyone explain what I'm seeing?

Niello answered 23/9, 2011 at 19:33 Comment(4)
Wow, that's a really weird bug you found (and yes, I'm 100% sure it is a bug :-) At least it has a really simple workaround, like you posted yourself, just name the Grid as well and you're good to goExcursus
Did you look/post at MS-connect?Luciferase
I've not posted to Connect yet, I did however search it as best I could.Niello
IsItmemsHost="True" affects the behaviour of items generation in ItemsControl and should be considered a must in any ItemsPanelTemplate. As to why x:Name affects the behaviour - I'm still baffled by that one....Foreandaft
A
0

I don't believe this is a bug. What you have done here is inadvertently introduced a bug in your code that does not throw an exception at design or compile time but at run-time, hence your animation did not complete and the content of the grid was not rendered.

The conflict is in the WPF XAML Namescope particular to DataTemplate as described in this MSDN article. The article does state that named elements within a template is automatically given a unique Namescope to prevent the name conflict, but it does not tell us what happens when the template root is unnamed but contains named children or how the IsItemsHost affects the ItemsTemplate.

  1. One approach I can suggest is to use your original scenario and during debugging, keep an eye on your output log for any runtime exceptions that has been quietly nerfed by the XAML parser related to your animation.

  2. Another (personally I think, better) approach would be to create a new 'templated control' that inherits from the ItemsControl. Here, you can access the ItemsPanel during the ApplyTemplate over-load to find your Grid/GridColumn and perform the animation there. Here is a good tutorial.

Amide answered 28/11, 2011 at 23:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.