42

使用时如何使每个ListView项目的内容扩展到 100% 宽度DataTemplate

HorizontalContentAlignment="Stretch"ListViewHorizontalAlignment="Stretch"中尝试过DataTemplate,但似乎没有任何效果,内容仍然与左侧对齐。

我有这样的事情:

<ListView x:Name="questionsView" Background="{StaticResource ApplicationPageBackgroundThemeBrush}" HorizontalContentAlignment="Stretch">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Border Background="BlueViolet" HorizontalAlignment="Stretch">
                <Grid HorizontalAlignment="Stretch">
                    <TextBlock Text="{Binding}" />
                    <TextBlock HorizontalAlignment="Right">16 minutes ago</TextBlock>
                </Grid>
            </Border>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

ListView我想在和之间还有一层ItemTemplate

4

3 回答 3

95

我知道了。ListView.ItemContainerStyleHorizontalContentAlignment二传手设置是诀窍。IE:

<ListView x:Name="questionsView" Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Border Background="BlueViolet">
                <Grid HorizontalAlignment="Stretch" Margin="0">
                    <TextBlock Text="{Binding}" />
                    <TextBlock HorizontalAlignment="Right">16 minutes ago</TextBlock>
                </Grid>
            </Border>
        </DataTemplate>
    </ListView.ItemTemplate>
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
    </ListView.ItemContainerStyle>
</ListView>
于 2013-09-05T02:44:27.527 回答
7

将项目容器的 Horizo​​ntalContentAlignment 属性设置Stretch,试试这个

<ListView.ItemContainerStyle>
   <Style TargetType="ListViewItem">
       <Setter Property="HorizontalContentAlignment" Value="Stretch" />
   </Style>
</ListView.ItemContainerStyle>
于 2014-05-29T07:36:42.273 回答
5

这里重要的是ScrollViewer.HorizontalScrollBarVisibility,TextWrappingItemContainerStylewith HorizontalContentAlignment。其余的都是绒毛。

<ListView VerticalAlignment="Stretch"
          ScrollViewer.VerticalScrollBarVisibility="Auto"
          ScrollViewer.HorizontalScrollBarVisibility="Disabled">

    <ListView.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Prop1}" TextWrapping="Wrap"/>
        </DataTemplate>
    </ListView.ItemTemplate>

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

</ListView>
于 2017-05-03T21:09:40.480 回答