none
ListView的内容不能填满整个空间,右边总有空隙 RRS feed

  • 问题

  • 我的ListView如下:

    <ListView x:Name="ItemListView" ItemTemplate="{StaticResource MessageListImageTemplate}" Width="426" Height="768" Margin="0,0,0,0"                             
                                  BorderThickness="0" VerticalAlignment="Stretch" HorizontalAlignment="Left"
                                  ScrollViewer.VerticalScrollBarVisibility="Auto"
                                  ScrollViewer.HorizontalScrollBarVisibility="Disabled" SelectionMode="Extended"
                                  SelectionChanged="ItemListView_SelectionChanged" Background="#F4F4F4F4">
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            </Style>
        </ListView.ItemContainerStyle>
    </ListView>

    使用的ItemTemplate如下:

    <DataTemplate x:Key="MessageListImageTemplate">
        <Grid Name="ListViewItemGrid" Width="426" Height="60"  Margin="0,0,0,0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Image Name="ReadImage" Source="Assets/smsread.png" Width="30" Height="30" Grid.Column="0" HorizontalAlignment="Left" Margin="20,0,10,0" VerticalAlignment="Center" />
            <Grid Name="ListViewItemStackPanel" Grid.Column="1" HorizontalAlignment="Left" Margin="5,0,0,0">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <StackPanel Grid.Row="0" Orientation="Horizontal">
                    <TextBlock Name="TextBlockNumber" Text="{Binding Title}" TextWrapping="Wrap" TextTrimming="WordEllipsis" Foreground="Black" Style="{StaticResource SubHeaderTextStyle}" Width="auto" Height="auto" Margin="0,10,0,0" FontFamily="Segone WP" FontSize="24"/>
                    <TextBlock Name="TextBlockDate" Text="{Binding Subtitle}" TextTrimming="WordEllipsis" Foreground="Black" Style="{StaticResource SmallContentFontStyle}" Width="auto" Height="auto" Margin="20,10,0,0" FontSize="24"/>
                </StackPanel>
                <TextBlock Name="TextBlockContent" Grid.Row="1" Text="{Binding Content}" TextTrimming="WordEllipsis" Foreground="Black" Style="{StaticResource SmallContentFontStyle}" Width="auto" Height="auto" FontSize="21" Margin="0,0,0,0" VerticalAlignment="Bottom"/>
            </Grid>
        </Grid>
    </DataTemplate>

    我的ListView和ItemTemplate的width都是426,按道理Item应该在width方向上填满Listview的宽426的空间,但是出来的效果是,每个Item的宽度其实只有400左右,在Item的左边有一条很窄的白边,在Item的右边有一条比较宽的白边,麻烦问一下大神是怎么回事?

    2012年10月30日 11:38

答案

  • 参见:http://social.msdn.microsoft.com/Forums/zh-CN/winappswithcsharp/thread/c63e9cc9-e51b-47c8-9072-0bf8d81cf009中,给Listview添加一条

    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            <Setter Property="Margin" Value="0,0,0,0"/>
        </Style>
    </ListView.ItemContainerStyle>

    原来这里默认的样式的Margin=0,0,18,2,设置为全0之后,基本上可以满足要求了,但是在两侧还是有一条很窄的白边,望解答!

     

    2012年10月31日 6:47
  • 需要修改ListView本身的样式模板了,你在VS 2012的Designer中右击ListView 选择Edit Template > Edit a copy 然后在生成的ListView的样式中修改外部ItemsPresenter的Padding值. 或者Margin Padding中的值都可以设为负数的。


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    2012年10月31日 9:56
    版主

全部回复