1

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?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Cris
  • 12,124
  • 27
  • 92
  • 159
  • This is possibly a duplicate question. I think the answer you are looking for at [How to get a ListBox ItemTemplate to stretch horizontally the full width of the ListBox?](http://stackoverflow.com/questions/838828/how-to-get-a-listbox-itemtemplate-to-stretch-horizontally-the-full-width-of-the). – Steve Konves Oct 08 '12 at 22:17

2 Answers2

2

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

failedprogramming
  • 2,532
  • 18
  • 21
-1

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

Community
  • 1
  • 1
paparazzo
  • 44,497
  • 23
  • 105
  • 176