Karine Bosch’s Blog

On SharePoint

Silverlight control not stretching to its full width when hosted in a SharePoint Web Part (continued)


When creating the sample I first tried

         <TextBlock x:Name="MarqueeTextBox" Foreground="White" FontFamily="Comic Sans MS"
                   FontSize="16" FontStyle="Italic" Width="Auto" Height="Auto" Padding="5"            
                   HorizontalAlignment="Stretch" VerticalAlignment="Stretch" TextWrapping="Wrap"                   
                   Text="This is a very long text that will be stretched automatically when the width of the containing web part changes."/>

but this didn’t make the text wrap through the canvas. This is because you need to specify a fixed width to make wrapping happen. If you want your text wrap through the canvas while stretching when the web part width changes, you have to add a little more code. First I changed the Width property to a fixed width:

         <TextBlock x:Name="MarqueeTextBox" Foreground="White" FontFamily="Comic Sans MS"
                   FontSize="16" FontStyle="Italic" Width="200" Height="Auto" Padding="5"            
                   HorizontalAlignment="Stretch" VerticalAlignment="Stretch" TextWrapping="Wrap"                   
                   Text="This is a very long text that will be stretched automatically when the width of the containing web part changes."/>

In code behind I defined a ResizeTextBlock method. This method resizes the width of the TextBlock and is executed when the Silverlight application loads. You also need to add an event handler for the SizeChanged event of the Silverlight control to ensure that the TextBlock is resized each time the web part width changes.

    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            ResizeTextBlock();
            this.SizeChanged += new SizeChangedEventHandler(MainPage_SizeChanged);
        }
        void MainPage_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            ResizeTextBlock();
        }
    }

First i t ried to set the width of the TextBlock to the width of the root canvas:

As you can see, the LayoutRoot has no actual width because no Width property has been defined on the canvas. The same problem occurs when you want to set the width of the TextBlock to the width of the Silverlight control itself, using this.Width. You have to rely on the width that has been set on the web part. You can grab the actual width of the plug-in display area represented by App.Current.Host.Content. Be aware that the ActualWidth property and the ActualHeight property do not contain meaningful values until after the Content.Resized event occurs for the first time.

        private void ResizeTextBox()
        {
            if (App.Current.Host.Content.ActualWidth > 0)
            {
                MarqueeTextBox.Width = App.Current.Host.Content.ActualWidth;
            }
        }

The text now wraps over the width of the canvas:

 

July 25, 2010 - Posted by | SharePoint 2007, SharePoint 2010, Silverlight

9 Comments »

  1. Have you figured out a way to make a Silverlight control stretch to the full height of its contents when hosted in a SharePoint 2007 web part? The only way it seems to work is if you give it a fixed height, which of course is not very flexible. I am becoming very frustrated with this problem.

    Any ideas?

    Thanks,
    Larkin

    Comment by Larkin | July 28, 2010 | Reply

    • That works if you set an explicit height for the web part. What I am trying to do is to allow the web part to stretch so that the entire contents of the Silverlight control are shown. For example, with a Silverlight grid control I have no way of knowing how tall the control needs to be, since the number of databound items will vary and the content of the cells is also a variable. Yes, I could set a height that would allow all the content to fit most of the time, but then I am wasting a lot of space in the event that only a few items are bound to the grid. I don’t want to specify any fixed height for the web part, and subsequently I want the Silverlight control to size itself according to the height that is necessary to display all the databound items, and not any taller. THe problem seems to be that if you don’t specify a fixed height for the web part, the Silverlight control thinks that the host div has a height of 0px, and so it doesn’t display at all. I used some example code from here:

      http://weblogs.manas.com.ar/spalladino/2008/08/05/silverlight-loaded-events/

      This allows me to handle an event when all child elements have sized themselves so I can get the DesiredSize.Height property. Unfortunately, of course, since the container div has a height of 0px this returns 0.

      I’m at a loss, and in all honesty I’m surprised this isn’t a more commonly reported problem, as the ability to build fluid layouts in an RIA is quite important, and unlike the ideal world set forth in the test page that Visual Studio generates to test a Silverlight app, you can’t always have every single parent element in the visual tree set with height=100%.

      Thanks in advance for your help,

      Larkin

      Comment by Larkin Young | July 28, 2010 | Reply

  2. Hi Larkin,
    Now I undestand your problem better. My sample shows how the Silverlight application can resize itself based on the web part with and height, while you want the other way around.
    In one of the last paragraphs yu state that the DesiredSize.Height property returns 0 because the containing div has a height of 0px but can’t you start with a max height on the div and then making it smaller by using JavaScript? You can handle Silverlight events in Javascript and a web part has a unique id, which you can store in a hidden field and retrieve from the Javascript event handler.
    Could this be a solution?
    Karine

    Comment by Karine Bosch | July 28, 2010 | Reply

    • Hi Karine, would you have an example of doing this, handling silverlight events from javascript to resize the web part. Larkin did you work this out, love to see how this was resolved.

      Comment by Robin | February 18, 2013 | Reply

      • Hi Robin, Unfortunately not. I haven’t been playing with Silverlight since 2011.

        Comment by Karine Bosch | February 19, 2013

  3. We have same problem, when hosting silverlight in a SharePoint 2007 web part.

    web part should stretch so that the entire contents of the Silverlight control are shown and browser should be responsible for scrolling not Silverlight.

    Any suggestion

    Best Regards,
    Shashi

    Comment by Shashidhara | August 31, 2010 | Reply

  4. Hi Karine,

    I went through all comments. Now I am setting height, width on DIV which hosts silverlight to 100% however when we set SharePoint WebPart property height, width to ” No. Adjust to zone”, the Div starch to some height, width which might be 150×150 which is not sufficient for silverlight content. When I try to get height, width of webpart, it is blank.

    So In my case getting that height using “App.Current.Host.Content.ActualWidth” will not help.

    Any thoughts how to get actual webpart height, width when set to “No. Adjust to zone.”

    Vishal

    Comment by Vishal Gurav | February 6, 2011 | Reply

  5. Hi Vishal,
    If you know the height of your Silveright application (from within your Silverilght app) you can call a javascript function that resets the height of your web part.
    Karine

    Comment by Karine Bosch | February 8, 2011 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: