Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a WPF data template fill the entire width of the listbox?

I have a ListBox DataTemplate in WPF. I want one item to be tight against the left side of the ListBox and another item to be tight against the right side, but I can't figure out how to do this.

So far I have a Grid with three columns, the left and right ones have content and the center is a placeholder with it's width set to "*". Where am I going wrong?

Here is the code:

<DataTemplate x:Key="SmallCustomerListItem">     <Grid HorizontalAlignment="Stretch">         <Grid.RowDefinitions>             <RowDefinition/>         </Grid.RowDefinitions>         <Grid.ColumnDefinitions>             <ColumnDefinition/>             <ColumnDefinition Width="*"/>             <ColumnDefinition/>         </Grid.ColumnDefinitions>         <WrapPanel HorizontalAlignment="Stretch" Margin="0">             <!--Some content here-->             <TextBlock Text="{Binding Path=LastName}" TextWrapping="Wrap" FontSize="24"/>             <TextBlock Text=", " TextWrapping="Wrap" FontSize="24"/>             <TextBlock Text="{Binding Path=FirstName}" TextWrapping="Wrap" FontSize="24"/>          </WrapPanel>         <ListBox ItemsSource="{Binding Path=PhoneNumbers}" Grid.Column="2" d:DesignWidth="100" d:DesignHeight="50"      Margin="8,0" Background="Transparent" BorderBrush="Transparent" IsHitTestVisible="False" HorizontalAlignment="Stretch"/>     </Grid> </DataTemplate> 
like image 943
Eric Haskins Avatar asked Sep 25 '08 19:09

Eric Haskins


2 Answers

I also had to set:

HorizontalContentAlignment="Stretch" 

on the containing ListBox.

like image 71
Eric Haskins Avatar answered Sep 18 '22 08:09

Eric Haskins


<Grid.Width>     <Binding Path="ActualWidth"               RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type ScrollContentPresenter}}" /> </Grid.Width> 
like image 29
Taeke Avatar answered Sep 19 '22 08:09

Taeke