开发者

Grid with named RowDefinition fails to populate as ItemsPanel in WPF4

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"开发者_Go百科 />
            <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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜