修改GridView模板时查看完整的网格项

时间:2012-10-10 17:42:17

标签: gridview windows-8 microsoft-metro datatemplate

我有这个xaml代码:

                <local:MyGridView x:Name="topDeal" ItemsSource="{Binding}" Margin="60,0,0,0" Grid.Row="1">
                    <GridView.ItemTemplate> 
                        <DataTemplate>
                            <Grid Margin="0">
                                <Grid.Background>
                                    <ImageBrush ImageSource="{Binding image1}"/>
                                </Grid.Background>

                                <StackPanel VerticalAlignment="Bottom" Background="#0072B0" Opacity="0.6">
                                    <Border BorderBrush="LightGray" BorderThickness="0,0,0,1"/>
                                    <TextBlock Text="{Binding name}" Foreground="white"/>
                                </StackPanel>
                            </Grid>
                        </DataTemplate>
                    </GridView.ItemTemplate>
                    <GridView.ItemsPanel>
                        <ItemsPanelTemplate>
                            <VariableSizedWrapGrid ItemHeight="180" ItemWidth="180" Orientation="Horizontal" MaximumRowsOrColumns="4"/>
                        </ItemsPanelTemplate>
                    </GridView.ItemsPanel>
                </local:MyGridView>

和c#代码背后:

public class MyGridView : GridView
{

    protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
    {
        var gi = element as GridViewItem;
        //First griditem of MyGridView will be modified
        if (this.Items.IndexOf(item) == 0)
        {
            gi.SetValue(VariableSizedWrapGrid.ColumnSpanProperty, 2);
            gi.SetValue(VariableSizedWrapGrid.RowSpanProperty, 2);
        }
        base.PrepareContainerForItemOverride(gi, item);
    }
}

我想让第一件商品以其他商品的双倍尺寸展示,其中包含背景图片。这个Link的结果。背景图片只填写了TextBlock。

enter image description here

1 个答案:

答案 0 :(得分:1)

在您的数据模板中,您可以尝试这种方式,如果我没错,它应该适合您:

`<Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}" Margin="0,0,0,80"><Image Source="{Binding Image}" AutomationProperties.Name="{Binding Title}" Stretch="UniformToFill" Height="Auto" Width="Auto"/></Border>`