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 
 

About Jesse Liberty

Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen Pluralsight & LinkedIn Learning courses. He was a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Xamarin Certified Mobile Developer and a Xamarin MVP and a Microsoft MVP.
This entry was posted in z Silverlight Archives. Bookmark the permalink.