Skip to main content
Home Forums Silverlight Programming Programming with .NET - General ContentPresenter inside a ListBoxItem is preventing HorizontalAlignment to stretch
3 replies. Latest Post by Thomk on July 2, 2009.
(0)
RogerGu
Participant
870 points
255 Posts
05-14-2009 4:13 PM |
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
Twitter
Andrejt
Member
746 points
113 Posts
05-14-2009 5:53 PM |
Restyle ListBoxItem's default style to set its HorizontalContentAlignment property to Stretch. Apply the new style to your ListBox through the ItemContainerStyle property.
05-14-2009 6:20 PM |
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
8 points
4 Posts
07-02-2009 3:57 AM |
Pfew, thanks!! Workaround is perfect!