How to set attributes to autoresize width and height of silverlight control/page

by Viper 26. December 2008 07:02

When you add a new Silverlight User control (as new page) to your application, you get the following autogenerated shell for your page.


<UserControl x:Class="SilverDragon.HostInfomation"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="300" Height="400">
<Grid x:Name="LayoutRoot" Background="CadetBlue"></Grid>
</UserControl>


You will notice that Width and Height property has been set to an absolute value. Your immediate reaction is to change these values to some relative value like "100%" to auto resize to 100% of container. When you run the application you get run time error telling you that you have specified invalid value for Width/Height property. If you look at declaration of these property, you will find that data type for these properties is Double. The way to specify Auto value for these properties is to set these values to Double.Nan. That is microsoft way of saying that set these properties to unspecified and framework will take care of resizing this control with respect to the container. And if you want to specify this unspecified value in declaration of control, then its simple. Leave it unspecified. What this means is that remove declaration of these attributes in control declaration. Do not set these values to empty strings otherwise you will get a page or control with 0 size. So your declaration of page will look like following.


<UserControl x:Class="SilverDragon.HostInfomation"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid x:Name="LayoutRoot" Background="CadetBlue"></Grid>
</UserControl>
Give your advice to big bosses and make money

Views: 3492

Tags:

Silverlight | ASP.Net | Silverlight

Comments

8/13/2009 7:10:44 PM #

Invicta Mens Pro Diver

I would like to add your blog to my blogroll please tell me what anchor should I use?

Invicta Mens Pro Diver United States

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.5.1.7
Theme by Naveen Kohli

By Categories