I have a ListBox in which each Item is a Grid of three columns. The first and last columns are 50 wide, and in the middle there is a TextBlock, which should stretch along the remaining width of the ListBox. How can I do that? I tried to set HorizontalAlignment="Stretch" in ListBox HorizontalAlignment="Stretch" but the result was zero, but I don’t want to set a fixed width for TextBlock for obvious reasons. Here is the whole code:

 <ListBox HorizontalAlignment="Stretch" x:Name="tracksList" Grid.Row="3" Grid.Column="2"> <ListBox.ItemTemplate> <DataTemplate> <Grid Height="50" Margin="0 2 0 2" Background="Green"> <Grid.ColumnDefinitions> <ColumnDefinition Width="50"></ColumnDefinition> <ColumnDefinition></ColumnDefinition> <ColumnDefinition Width="50"></ColumnDefinition> </Grid.ColumnDefinitions> <TextBlock HorizontalAlignment="Stretch" Background="SeaGreen" Text="{Binding Path=fn}" Grid.Column="1"></TextBlock> </Grid> </DataTemplate> </ListBox.ItemTemplate> </ListBox> 

Maybe you need to somehow podshamanit with DataTemplate, waiting for your suggestions.

    1 answer 1

    Set HorizontalContentAlignment="Stretch" property on your ListBox
    Before:
    enter image description here
    After:
    enter image description here

    • so is worth me - Max
    • @Max, I don’t see this in your markup. HorizontalAlignment is, but there is no HorizontalContentAlignment - these are different things. Take a closer look. - Andrei NOP
    • @Max you stretch the element, not the content ;-) - user227049
    • My God, Thank you. It is already blind. - Max