Phil Murray
Phil Murray

Reputation: 6554

Silverlight 5 Listbox sizing

I have a Silverlight 5 Listbox inside a Star sized grid column and within the Listbox DataTemplate another Grid.

What I want to do is to fill the available space in the DataTemplate grid for the second column. As shown in the image below its only filling to the extend of the contained text.

How do I get the content to fill the available space?

<ListBox x:Name="TopFaultsListBox" Margin="2" ItemsSource="{Binding TopFaults, Mode=OneWay}" 
            ScrollViewer.HorizontalScrollBarVisibility="Disabled" SelectedIndex="-1" Background="{x:Null}" 
            BorderThickness="5" BorderBrush="#FFB1925C" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" VerticalAlignment="Stretch" VerticalContentAlignment="Stretch" >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid Background="#FFE29826">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <TextBlock x:Name="FailureCount" Foreground="Red" FontSize="40" Grid.Row="0" Grid.RowSpan="3" Text="{Binding FaultCount}" Margin="10"/>
                <TextBlock x:Name="AssemblyName" FontSize="20" Text="{Binding AssemblyName}" Grid.Column="1" Grid.Row="1"/>
                <TextBlock x:Name="FaultInformation" FontSize="20" Text="{Binding FaultInformation}" Grid.Column="1" Grid.Row="2"/>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

enter image description here

Upvotes: 2

Views: 163

Answers (1)

Jehof
Jehof

Reputation: 35554

You need to specify the HorizontalContentAlignment in the ItemContainerStyle of the ListBox and set it to Stretch. The default value is Left.

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

Upvotes: 3

Related Questions