当ItemsPanel设置为Grid时,GridView会意外渲染

时间:2013-06-13 15:21:18

标签: xaml windows-store-apps

我在页面上有以下GridView:

<GridView Background="Black">
    <GridView.ItemsPanel>
        <ItemsPanelTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
            </Grid>
        </ItemsPanelTemplate>
    </GridView.ItemsPanel>
    <GridView.Items>
        <TextBlock Grid.Row="0" Grid.Column="0" Text="Hello"/>
        <TextBlock Grid.Row="1" Grid.Column="1" Text="there"/>
    </GridView.Items>
</GridView>

这会在左上象限中将“Hello”和“there”呈现在彼此的顶部。我原本希望在左上象限中找到“Hello”,在右下象限中找到“there”。

我错过了什么?

1 个答案:

答案 0 :(得分:3)

框架将自动创建一个GridViewItem以包含GridView中的每个项目,除非您明确创建一个。因此,对所包含项目设置Grid.RowGrid.Column将无效。试试这个:

<GridView Background="Black">
    <GridView.ItemsPanel>
        <ItemsPanelTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
            </Grid>
        </ItemsPanelTemplate>
    </GridView.ItemsPanel>
    <GridView.Items>
        <GridViewItem Grid.Row="0" Grid.Column="0">
            <TextBlock Text="Hello"/>
        </GridViewItem>
        <GridViewItem Grid.Row="1" Grid.Column="1">
            <TextBlock Text="there"/>
        </GridViewItem>
    </GridView.Items>
</GridView>