Changing the width occupied by the Silverlight control

Today a friend of mine asked me what he needed to do to change the width occupied by the silverlight control. He knew that he could set up the width of the control by setting the width: he just didn”t know how to change its width after having created it. He even searched the docs and, as I was also able to see, the Silverlight control doesn”t expose a Width property (there”s only a ActualWidth property, which is readonly).

Sometimes, the solution is right there and you can”t see it. That”s why i”m writing this post…

When you think about it, the Silverlight control is an object which occupies space on the page. There is already a standard way to change the width of an element (normally, an HTML control) on a page: you use the width style property. Yes, that”s correct. if you have a silverlight control called SilverlightControl, then you can change its width with the following code:

var ctl = document.getElementById(“SilverlightControl”); = “600px”;

In this case, I”m just setting the width to 600 pixels. Oh, if like me, you”re using the alpha version,then you”ll want to handle the resizing of your host control. In order to do that,you should handle the Resizing event of the BrowserHost object:

BrowserHost.Resize += new EventHandler( OnResize );

And then, you can use the ActualWidth and ActualHeight properties to get the current width and height values:

private void OnResize(object sender, EventArgs e)
{ // top canvas partial method
            this.Width = BrowserHost.ActualWidth;
            this.Height = BrowserHost.ActualHeight;

Simple, right?


~ by Luis Abreu on June 11, 2007.

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: