ListBoxItem not stretching in ListBox

I am creating a Windows Phone 8 (silverlight) app. I want to display data in two columns in a ListBox. I am using this method . This article presents a demo project with code. Here's the basic code for the output:

<ListBox x:Name="ItemsListBox">
<ListBox.ItemTemplate>
    <DataTemplate>
        <ItemsControl ItemTemplate="{StaticResource ItemDataTemplate}" ItemsSource="{Binding}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
    </DataTemplate>
</ListBox.ItemTemplate>

      

This works, but the data is not stretched, on the right is free space. enter image description here

I have tried many options

<ListBox...>
<ListBox.Resources>
    <Style TargetType="ListBoxItem">
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    </Style>
</ListBox.Resources>

      

....

inserted HorizontalAlignment = "Stretch"

but they don't work. Tell me how do I stretch the width of the data? I need each column to take up half of the total width.

This is the code of my DataTemplate

<DataTemplate x:Key="ItemDataTemplate" >


        <Grid HorizontalAlignment="Stretch">
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition Width="4*"/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>




            <Button Grid.Column="1" Margin="5" HorizontalContentAlignment="Stretch" HorizontalAlignment="Stretch" >
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition Width="4*"/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>

                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>

                    <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Name}"  />
                    <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding AddInfo}"  />
                </Grid>
            </Button>

        </Grid>


</DataTemplate>

      

+3


source to share


1 answer


In the data template you have to add Height = "100%" for vertical stretch or width = "100%" for horizontal stretch inside the text box



</Grid.RowDefinitions>

                <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Name}" Height="//some vale in decimal" Widht="//some vale in decimal" />
                <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding AddInfo}" Height="//some vale in decimal" Widht="//some vale in decimal" />
            </Grid>

      

0


source







All Articles