Josh Brass
Josh Brass

Reputation: 147

ListView ItemTemplate Grid 100% Width

I have made an ListView ItemTemplate and I want it to be responsive (when orientation changes, for example, the listView item changes in size). I am using a Grid as a control for the inner elements of the grid but it is not behaving. The ListView.ItemContainerStyle has property HorizontalAlignment="Stretch" which is the behaviour I want, and the ItemContainerStyle is the correct width. Inside the Border and Grid I have the same HorizontalAlignment="Stretch" and they are overflowing when the TextBox contained inside has lots of text, and when there is little or no text in the TextBox the Border element shrinks to be smaller than the ItemContainerStyle is showing.

<ListView ItemsSource="{Binding TileStories}" x:Name="cont" Margin="0,10,0,10" Background="{StaticResource CustomResourceBrush}" BorderBrush="{StaticResource CustomResourceBrush}" Foreground="{StaticResource CustomResourceBrush}">
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                <Setter Property="Margin" Value="20,10,20,10" />
                <Setter Property="Foreground" Value="{StaticResource BTVioletBrush}" />
            </Style>
        </ListView.ItemContainerStyle>
        <ListView.ItemTemplate>
            <DataTemplate>
                <Border CornerRadius="20" BorderThickness="0" Width="{Binding ScrollViewerWidth}" Background="White" HorizontalAlignment="Stretch">
                    <StackPanel Height="160" Orientation="Horizontal">
                        <Grid Background="black">
                            <TextBox Text="Example">
                        </Grid>
                    </StackPanel>
                </Border>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

Upvotes: 1

Views: 979

Answers (4)

Samuel Marvin Aguilos
Samuel Marvin Aguilos

Reputation: 165

Just do this

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

Upvotes: 2

Josh Brass
Josh Brass

Reputation: 147

I didn't exactly find a solution but I did find a workaround. The Grid was being bound with Width="0" if I used {Binding ActualWidth, ElementName=StackPanel, Mode=OneWay} where StackPanel was the panel within the data template. I was poking around the designer in VS2013 and figured out that the Width was 0 because (I am assuming) the items in the data template are drawn one by one and therefore the Width was zero when the first template was drawn and so on and so forth. I haven't explained that very well I guess, but the important thing is the solution:

<PivotItem x:Name="Feed">
    ....
    <Border CornerRadius="20" BorderThickness="0" Background="White" HorizontalAlignment="Stretch">
        <StackPanel Height="160" Orientation="Horizontal">
            <Grid HorizontalAlignment="Stretch" Width="{Binding ActualWidth, ElementName=Feed, Mode=OneWay}">
            ........
            </Grid>
        </StackPanel>
    </Border>
...
</PivotItem>

I think that the PivotItem having a non-variable Width meant the Grid had a concrete Width to inherit from.

Upvotes: 0

Tomasz Pikć
Tomasz Pikć

Reputation: 763

Add to your ItemContainerStyle

            <Setter Property="HorizontalContentAlignment"
                    Value="Stretch" />

And I think Width="{Binding ScrollViewerWidth}" is not required. You can remove this.

Upvotes: 0

thang2410199
thang2410199

Reputation: 1942

Define MinHeight as 0 for ItemContainerStyle

Upvotes: 0

Related Questions