Cris
Cris

Reputation: 12204

How can a ListBox fill its parent width?

I have a ListBox and need that it fills the parent's width but have not found a way to do it, the ListBox has always the ListBox width and not the 100%.

Here is my XAML code:

<ListBox ItemsSource="{Binding anagSearchResults}" BorderThickness="0" Background="Gray"
     SelectedItem="{Binding selectCustomer}" 
     FontSize="14"    
     ScrollViewer.HorizontalScrollBarVisibility="Disabled">
     <ListBox.ItemContainerStyle>
           <Style TargetType="ListBoxItem">
               <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
           </Style>
     </ListBox.ItemContainerStyle>
     <ListBox.ItemTemplate>
         <DataTemplate>
               <Grid HorizontalAlignment="Stretch">
                   <Grid.ColumnDefinitions>
                       <ColumnDefinition Width="*"></ColumnDefinition>
                       <ColumnDefinition Width="*"></ColumnDefinition>
                       <ColumnDefinition Width="*"></ColumnDefinition>
                       <ColumnDefinition Width="*"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Column="0" Text="{Binding FirstName}"></TextBlock>
                    ....

What am I doing wrong?

Upvotes: 1

Views: 1864

Answers (2)

failedprogramming
failedprogramming

Reputation: 2522

I would check the HorizontalAlignment of the parent(s) and make sure none of them are set to left, right or centre.

Upvotes: 2

paparazzo
paparazzo

Reputation: 45106

I would go with a ListView GridView.
Then for width you need to use a converter.
Change the converter in this sample to be parent width / 4

enter link description here

See the checked answer from me

Upvotes: -1

Related Questions