Skip to main content

Microsoft Silverlight

Answered Question ContentPresenter inside a ListBoxItem is preventing HorizontalAlignment to stretchRSS Feed

(0)

RogerGu
RogerGu

Participant

Participant

870 points

255 Posts

ContentPresenter inside a ListBoxItem is preventing HorizontalAlignment to stretch

I used Silverlight Spy to track down my issue to what I am pretty certain is the problem. I have a custom UserControl (OpportunitySummary in image below) as the DataTemplate of an ItemTemplate for a ListBox. The custom UserControl should stretch/grow to fit what parent it is placed in. However it is not in this case. When I use Silverlight Spy to increase the width of the ContentPresenter (note that he content presenter is added by the ListBoxItem, to hold my custom control), the custom usercontrol 'stretches' as I want it to.

So, the question: How can I prevent the ContentPresenter inside the ListBoxItem from from inhibiting the size of my usercontrol?

 

Thanks,

Roger

Andrejt
Andrejt

Member

Member

746 points

113 Posts

Silverlight MVP
Answered Question

Re: ContentPresenter inside a ListBoxItem is preventing HorizontalAlignment to stretch

Restyle ListBoxItem's default style to set its HorizontalContentAlignment property to Stretch. Apply the new style to your ListBox through the ItemContainerStyle property.

Andrej Tozon
MVP - Client Application Development | Blog | Twitter

RogerGu
RogerGu

Participant

Participant

870 points

255 Posts

Re: ContentPresenter inside a ListBoxItem is preventing HorizontalAlignment to stretch

Yes, that was it, even though HorizontalContentAlignment was ignored on the ListBox tag. Thanks!  For anyone else, here is how that looks:

	<Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem">
		<Setter Property="HorizontalContentAlignment" Value="Stretch" />
	</Style>
	<ListBox ItemContainerStyle="{StaticResource ListBoxItemStyle}" HorizontalContentAlignment="Stretch" Grid.Row="1" ItemsSource="{Binding Mode=OneWay, Path=TestAccounts, Source={StaticResource TestDataDS}}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <controls:OpportunitySummary />
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
 

Thomk
Thomk

Member

Member

8 points

4 Posts

Re: ContentPresenter inside a ListBoxItem is preventing HorizontalAlignment to stretch

Pfew, thanks!! Workaround is perfect!

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities