Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView ItemTemplate 100% width

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.

like image 540
kiewic Avatar asked Sep 05 '13 02:09

kiewic


People also ask

How to display data items in listview using itemtemplate?

Use the ItemTemplate property to define a custom user interface (UI) to display the data items. The ItemTemplate template is required by the ListView control. It usually contains controls to display the field values of a record.

How do I add a custom template to a listview?

To specify the custom template declaratively, add an ItemTemplate element inside the ListView control. You can then add the contents of the template to the ItemTemplate element. To display the field values of the data source that is bound to the control, use a data-binding expression.

How to fix listview items not filling full width?

ListView with items not filling full width. It should be noted that despite <Grid HorizontalAlignment=”Stretch”>, that produces no effect to make the items occupied the entire width of the list. The solution is to set a style to the ItemContainerStyle for the ListViewItem with the HorizontalContentAlignment=”Stretch”, as exemplified below:

What are the parts of a listview control?

The ListView control does not have any named parts. When you create a ControlTemplate for a ListView, your template might contain an ItemsPresenter within a ScrollViewer. (The ItemsPresenter displays each item in the ListView; the ScrollViewer enables scrolling within the control).


2 Answers

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>
like image 95
kiewic Avatar answered Oct 12 '22 08:10

kiewic


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>
like image 9
Khurram Avatar answered Oct 12 '22 07:10

Khurram