Support auto width in Silverlight web part
For some reason, the default implementation of the Silverlight webpart which comes with VS2010 doesn’t allow you to set the width to 100% (auto width). You’ll get an error saying “UnitType Percentage is not supported”. That’s kind of weird, since the only thing it really does is set the width for the surrounding object container (check your HTML source to see what I mean).
The object container is rendered by the SilverlightPluginGenerator class which is automatically added in every new SharePoint web part project. So we can just extend that class to support percentages too, it’s real simple. Add the following line of code at line 182:
case UnitType.Percentage: ret = "%"; break;
That’s it, you’re good to go. Now, on creating the webpart (SilverlightWebPart.cs), set the following:
this._silverlightPluginGenerator = new SilverlightPluginGenerator
Source = new Uri(“/SiteCollectionDocuments/Silverlight/APDashBoard/APDashBoard.xap”, UriKind.Relative),
Width = new Unit(100, UnitType.Percentage),
Height = new Unit(450, UnitType.Pixel),
BackGround = Color.White,
Version = SilverlightVersion.v3,
AutoUpgrade = true,
OnError = “onSilverlightError”,
And your webpart will auto fill the horizontal space.