ListViewItem未扩展到页面的宽度

时间:2014-05-21 00:42:40

标签: c# xaml windows-8 windows-runtime winrt-xaml

我正在定义一个像这样的ListView:

    <DataTemplate x:Key="LibraryItemTemplate">
        <Grid Height="191"
              UseLayoutRounding="True">
            <Grid.Background>
                <ImageBrush Stretch="Fill"
                            ImageSource="Assets/BookShelf.jpg" />
            </Grid.Background>

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>

            <Grid x:Name="gridTitle"
                  Background="{Binding Text, Converter={StaticResource LibraryItemBackgroundConverter}, ElementName=tbTitle}"
                  Margin="6,4,6,13">

                <TextBlock x:Name="tbTitle"
                           TextWrapping="Wrap"
                           VerticalAlignment="Center"
                           RenderTransformOrigin="0.5,0.5"
                           Width="100"
                           Margin="0,0,0,0.2"
                           TextAlignment="Center"
                           FontSize="24"
                           FontWeight="Bold"
                           UseLayoutRounding="False"
                           d:LayoutRounding="Auto">
                    <TextBlock.RenderTransform>
                        <CompositeTransform Rotation="-90" />
                    </TextBlock.RenderTransform>
                    <Run Text="{Binding Title}" />
                </TextBlock>
            </Grid>

            <Grid x:Name="gridBooks"
                  Grid.Column="1"
                  Margin="0">

                <GridView x:Name="booksGridView"
                          AutomationProperties.AutomationId="ItemGridView"
                          AutomationProperties.Name="Grouped Items"
                          ItemsSource="{Binding Items}"
                          ItemTemplateSelector="{StaticResource textbookTemplateSelector}"
                          SelectionMode="Multiple"
                          IsItemClickEnabled="True"
                          ItemClick="booksGridView_ItemClick"
                          SelectionChanged="booksGridView_SelectionChanged"
                          IsSwipeEnabled="false"
                          ScrollViewer.VerticalScrollBarVisibility="Auto">

                    <GridView.ItemsPanel>
                        <ItemsPanelTemplate>
                            <WrapGrid Orientation="Horizontal" />
                        </ItemsPanelTemplate>
                    </GridView.ItemsPanel>
                </GridView>
            </Grid>
        </Grid>
    </DataTemplate>


    <Grid Grid.Row="1"
          Margin="80,0,12,0">

        <ListView     x:Name="libraryListView"
                      AutomationProperties.AutomationId="VideoListView"
                      AutomationProperties.Name="Videos"
                      TabIndex="1"
                      Padding="0,0,4,0"
                      ItemsSource="{Binding}"
                      IsSwipeEnabled="False"
                      SelectionChanged="LibraryListView_SelectionChanged"
                      ItemTemplate="{StaticResource LibraryItemTemplate}"
                      ItemContainerStyle="{StaticResource LibraryListViewItemStyle}">
        </ListView>
    </Grid>

我遇到的问题是每个ListViewItem都有不同的宽度,基于GridView中的元素数量。

如何强制每个ListViewItem使用屏幕的最大宽度(以便&#34; Assets / BookShelf.jpg&#34;对于每个ListViewItems都是相同的。)

请参阅附图以更好地展示我的问题。

感谢enter image description here

2 个答案:

答案 0 :(得分:0)

为了给你最好的答案,我需要一个预期布局的虚拟截图,但听起来你想要正确的列拉伸,这可以通过将其宽度设置为星号(*)而不是自动(只占用所需的空间。

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" /> <!-- ONLY TAKE UP SPACE NEEDED -->
            <ColumnDefinition Width="*" /> <!-- TAKE UP ALL AVAILABLE SPACE -->
        </Grid.ColumnDefinitions>

答案 1 :(得分:0)

答案是这段代码:

<ListView ..>
  <ListView.ItemContainerStyle>
     <Style TargetType="ListViewItem">
        <Setter Property="HorizontalAlignment" Value="Strech" />
        .
        .
        .
        .
        <Setter Property="HorizontalContentAlignment" Value="Strech" />
     </Style>
   </ListView.ItemContainerStyle>
   ...
</ListView>

在这段代码中,我们设置了ItemContainerStyle:Horizo​​ntalAlignment =“Strech”和Horizo​​ntalContentAlignment =“Strech”,它就成了伎俩。