Skip to main content

Microsoft Silverlight

Answered Question Silverlight 2.0 RC0 the ContentControl is broken!RSS Feed

(0)

coughlinj
coughlinj

Member

Member

334 points

114 Posts

Silverlight 2.0 RC0 the ContentControl is broken!

 That may be an over reaction but the change they made to make it extend Framework element has totally messed up my layouts.

 Controls that once stretched to fill the complete screen are now sized only to the size of their content.  No more fluid layouts for me:)

 Please let me know if there is a work around or if this is indeed a bug.  I'm sure its related to the breaking change "ContentPresenter now derives from FrameworkElement instead of Control".

I've simplified this to the following app...

 

Page.xaml

----------------------

<UserControl x:Class="ContentTest.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:src="clr-namespace:ContentTest">
    <Grid x:Name="LayoutRoot" Background="Red">
        <ContentControl>
            <src:MyControl />
        </ContentControl>
    </Grid>
</UserControl>

 

 MyControl.xaml

---------------------

<UserControl x:Class="ContentTest.MyControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid x:Name="LayoutRoot">
        <Rectangle Fill="Black" Stretch="Fill" />
        <TextBlock Text="Hello World" Foreground="Wheat" />
    </Grid>
</UserControl>
 

----------------------

 Output:

Beta 2 - The Black Rectangle fills the entire browser window.

RC0 - The Block Rectangle covers only the Text "Hello World".  The Red background of the page.xaml control fills the remainder of the window.

hhristov
hhristov

Member

Member

20 points

5 Posts

Answered Question

Re: Silverlight 2.0 RC0 the ContentControl is broken!

This is the default behavior for ContentControl because the Style for ContentControl have these setters:

<Setter Property="HorizontalContentAlignment" Value="Left"/>

<Setter Property="VerticalContentAlignment" Value="Top"/>

 

So you have 2 options:

  1. Remove the surrounding ContentControl 
  2. Or leave the ContentControl there and set its HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch

Your XAML should look like this (in the 2nd case):

<Grid x:Name="LayoutRoot" Background="White">

<ContentControl HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">

<src:MyControl />

</ContentControl>

</Grid>

 

Hope this helps

Regards,

Hristo

coughlinj
coughlinj

Member

Member

334 points

114 Posts

Re: Silverlight 2.0 RC0 the ContentControl is broken!

 Perfect.  That's it.  I had tried changing the HorizontalAlignment but not the "ContentAlignment".  Thanks so much!

rfcdejong
rfcdejong

Member

Member

2 points

1 Posts

Re: Silverlight 2.0 RC0 the ContentControl is broken!

If i had just found this post earlier. It took me in total 3 hours to find out why my sandribbon wouldn't fit the browser size. Using CAG i have a ContentControl defined as Region and i was looking in the wrong corner. The default.aspx hosting page, the silverlight plugin control, the page itself.

I did look at the ContentControl but overlooked HorizontalContentAlignment, i tried HorizontalAlignment.

Anyway, what is the difference between those?

hhristov
hhristov

Member

Member

20 points

5 Posts

Re: Re: Silverlight 2.0 RC0 the ContentControl is broken!

HorizontalAlignment is for the ContentControl. What this means is that if are measured with lets say 100, 100 and you have HorizontalAlignment=Stretch then the ContentControl will return DesiredSize 100 (for Width). If you set HorizontalContentAlignment to Stretch then the content (or to be precise the container for this content which is ContentPresenter if the content is not UIElement or the UIElement) will take all the available width. But if you set HorizontalContentAlignment to Center then the ContentControl will take all available Width and the content will be Centered in this width.

 

I hope I've explained it well. Smile

Mark Stega
Mark Stega

Member

Member

11 points

16 Posts

Re: Re: Re: Silverlight 2.0 RC0 the ContentControl is broken!

 I actually have another thread (http://forums.silverlight.net/forums/p/132041/295723.aspx#295723) about this same issue. I have the content control as a Prism region.  I associate a view & its view-model with the content control (it is an owner control with a grid) and I can not get it to size to the full reqion even when I use the 'content' alignment as suggested above.

hhristov
hhristov

Member

Member

20 points

5 Posts

Re: Re: Re: Silverlight 2.0 RC0 the ContentControl is broken!

Hello Mark,

 

by default HorizontalAlignment and VerticalAlignment are Stretch for ContentControl so you shound not set the to stretch.

You should only set HorizontalContentAlignment and VerticalContentAlignment to stretch for ContentControl if you want its content stretched.

 

Let me know if this works for you.

Mark Stega
Mark Stega

Member

Member

11 points

16 Posts

Re: Re: Re: Re: Silverlight 2.0 RC0 the ContentControl is broken!

 I have HorizontalAlignment & VerticalAllignment as Center and the HorizontalContentAlignment & VerticalContentAlignment as Stretch.

The content is now horizontally centered, but vertically it is at the top and the content did not stretch so it appears not to have helped.

hhristov
hhristov

Member

Member

20 points

5 Posts

Re: Re: Re: Re: Silverlight 2.0 RC0 the ContentControl is broken!

Hello Mark,

Like I said you should not set VerticalAlignment to Stretch because it is the default value. You should set it only if you need to have other value (like Top, Center or Bottom).

If you want your content to be stretched verticaly you should set both VerticalAignment and VerticalContentAignment to stretch. Or just VerticalContentAignment to stretch (and don't  set VerticalAignment because by default it is Stretch).

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities