ListView ItemTemplate 100% width

流过昼夜 提交于 2019-12-18 10:48:26

问题


How can I make content of each ListView item expands to 100% width when using a DataTemplate?

I have tried HorizontalContentAlignment="Stretch" in the ListView and HorizontalAlignment="Stretch" in the DataTemplate, but nothing seems to work, content is still aligned to the left.

I have something like this:

<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>

I guess there is one more layer between the ListView and the ItemTemplate.


回答1:


I got it. Setting the ListView.ItemContainerStyle with a HorizontalContentAlignment setter makes the trick. I.e.:

<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>



回答2:


Set the item container's property of HorizontalContentAlignment to Stretch, try this

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



回答3:


What is important here is the ScrollViewer.HorizontalScrollBarVisibility, TextWrapping and ItemContainerStyle with HorizontalContentAlignment. The rest is fluff.

<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>


来源:https://stackoverflow.com/questions/18626696/listview-itemtemplate-100-width

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!