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

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>
comments powered by Disqus

Search

Social

Weather

7.2 °C / 45.0 °F

weather conditions Clouds

Monthly Posts

Blog Tags