Jesse Liberty - Silverlight Geek

By, For and About Silverlight Developers

Does Canvas.Height default to zero or auto?

In today's Tip of the Day I wrote that Canvas's Height and Width properties default to zero. A reader wrote to me in email saying,

Actually, unless it was implemented differently in Silverlight than in WPF (which I cannot exclude), the default width and height is Auto. In this, Canvas is no different from any other Panel (Width and Height of a WPF Grid also are Auto).

I will check with the folks who implemented Canvas, but for now I can tell you that the Silverlight documentation confirms that the values default to zero (though, as I wrote in my return email, I would agree that this is not dispositive).

I also wrote a test program, though I'm not certain enough about the internals to be sure that even this puts the matter to rest:

 

Scene.xaml

<Canvas xmlns="http://schemas.microsoft.com/client/2007"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   <Canvas Name="InnerCanvas"
           Background="LightBlue"
           >
      <Rectangle x:Name="myRect0"
                 Width="100"
                 Height="44"
                 Canvas.Left="0"
                 Canvas.Top="10"
                 StrokeThickness="2"
                 Stroke="Black"
                 Fill="Red" />
      <TextBlock Name="Message"
                 Width="200"
                 Height="200"
                 Text="Ready"
                 Canvas.Left="150"
                 Canvas.Top="10" />
   </Canvas>
</Canvas>

(Note that Canvas has no Height or Width set).

 

Scene.xaml.js

▬<Canvas xmlns="http://schemas.microsoft.com/client/2007"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   <Canvas Name="InnerCanvas"
           Background="LightBlue"
           >
      <Rectangle x:Name="myRect0"
                 Width="100"
                 Height="44"
                 Canvas.Left="0"
                 Canvas.Top="10"
                 StrokeThickness="2"
                 Stroke="Black"
                 Fill="Red" />
      <TextBlock Name="Message"
                 Width="200"
                 Height="200"
                 Text="Ready"
                 Canvas.Left="150"
                 Canvas.Top="10" />
   </Canvas>
</Canvas>
canvasDefaults 
 

Comments

swildermuth said:

He's right and wrong...Canvas in WPF is different...Height and Width are Auto in WPF...but there is no concept of Auto in Silvelright...so go figure!

# January 12, 2008 6:43 PM

rjstinyc said:

Although I am not an expert at Silverlight and consider myself a debutant coder, it may be desirable to have the same postitioning and size flexibilities with the canvas in Silverlight as is true with the background or a division in html.

Jesse, on another note how are you recovering from your slip on the ice last month?

Best Regards,

Richard

# January 13, 2008 6:24 AM