0

Hi I see there have been many questions about ListBox and filling the entire space for the DataTemplate but I still cannot get anything to work. What I have is a ListBox and the DataTemplate has a UserControl. How do I get my UserControl to stretch the data to fill the space?

MainWindow XAML snippet:

<ListBox x:Name="ConfiguredItemsList">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <local:ConfiguredItem DataContext="{Binding}" HorizontalContentAlignment="Stretch" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

UserControl XAML snippet:

<UserControl.Resources>
    <local:ImagePathConverter x:Key="ImagePathConverter"/>
</UserControl.Resources>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="75" />
    </Grid.ColumnDefinitions>
    <Grid Grid.Column="0">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <TextBlock Grid.Row="0" Grid.Column="0" x:Name="NameLabel" Text="Name:" />
        <TextBlock Grid.Row="0" Grid.Column="1" x:Name="tbName" Text="{Binding Path=Name}" />
        <TextBlock Grid.Row="1" Grid.Column="0" x:Name="DescriptionLabel" Text="Description: " />
        <TextBlock Grid.Row="1" Grid.Column="1" x:Name="Description" Text="{Binding Path=Description}" />
        <TextBlock Grid.Row="2" Grid.Column="0" x:Name="TimeLabel" Text="Time:" />
        <TextBlock Grid.Row="2" Grid.Column="1" x:Name="Time" Text="{Binding Path=ChangeTime, StringFormat={}{0:h:mm tt}}" />            
    </Grid>

    <Border Grid.Column="1" BorderThickness="1" BorderBrush="Black">
        <Image Source="{Binding Path=WallpaperInfo, Converter={StaticResource ImagePathConverter}}" />
    </Border>
</Grid>
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Travyguy9
  • 4,774
  • 8
  • 43
  • 63
  • 2
    Have you tried [something like this](http://stackoverflow.com/questions/6188371/fill-grid-width-inside-a-listbox/6188604#6188604) or what exactly are the questions you've seen that did not help? – H.B. Oct 27 '11 at 23:30
  • possible duplicate of [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) – Matt Hamilton Oct 27 '11 at 23:48
  • @MattHamilton Not duplicate, I have that in mine and it doesnt work. – Travyguy9 Oct 28 '11 at 00:17
  • @Travyguy Not in the code you posted, you don't. The HorizontalContentAlignment attribute should be on the ListBox element. – Matt Hamilton Oct 28 '11 at 00:21
  • try to edit the List Box Template then remove the padding and margin attributes of the item container – Allan Chua Oct 28 '11 at 03:03

1 Answers1

4

Have you tried to set HorizontalContentAlignment to stretch on list box level?

<ListBox x:Name="ConfiguredItemsList"  HorizontalContentAlignment="Stretch">
      <ListBox.ItemTemplate>
            <DataTemplate>
                <local:ConfiguredItem DataContext="{Binding}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
</ListBox>

Note: I have set HorizontalContentAlignment property of ListBox instead of Item.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
RockWorld
  • 1,278
  • 2
  • 11
  • 24