Setting the size of a silverlight window at runtime automatically?

Posted by dilbert789 on Stack Overflow See other posts from Stack Overflow or by dilbert789
Published on 2010-04-06T13:19:59Z Indexed on 2010/04/06 13:23 UTC
Read the original article Hit count: 134

Filed under:
|
|

I need to be able to change the size of my Silverlight object at run time because I have content that changes in size dramatically between pages, and I'd rather not show a Silverlight scroll bar inside the browser scroll bar.

I was able to do the following to get it to work:

In the aspx page hosting the silverlight control: - Set your Div to the starting size you need it to be - Set the Silverlight Obejct to width=”100%” height=”100%”

In your Main page add this:

public static void ChangeSize(string height)
    {
        var myDiv = HtmlPage.Plugin.Parent;
        while (myDiv.TagName != "div")
            myDiv = myDiv.Parent;

        myDiv.SetStyleAttribute("height", height);           
    }

Anywhere you want to now change the size of the page do this:

string newHeight = (App.Current.Host.Content.ActualHeight + 75).ToString();
MainPage.ChangeSize(newHeight + "px");
//App.Current.Host.Content.ActualHeight will get the current actual height of the object

Is there a way that I can set this to re-size automatically so that I do not have to specify the amount to change?

Currently using SL 2, interested in if there is another way in a different version though.

© Stack Overflow or respective owner

Related posts about Silverlight

Related posts about c#