简体   繁体   中英

c#/xaml background color doesn't show on auto width/height settings

I'm following the Head First C# book and got into a little bit of problem. It instructs to form a canvas with background color. I set the canvas width and height to auto which shows 0. But when I do this the background color doesn't show:

<Canvas x:Name="playArea" HorizontalAlignment="Center" Margin="0" Grid.Row="1" VerticalAlignment="Center" Grid.ColumnSpan="3" Background="White"/>

When I set the width and height to any number, the color shows (in respectable size). Why can't I see the background color on auto settings?

When you set HorizontalAlignment="Center" your Canvas will try to take up as little horizontal space as possible (ActualWidth), and place itself in the middle of its container.

In your case, ActualWidth will be ZERO, because the Canvas has nothing inside of it that would require more than that.

So the reason for the problem you describe is that the Canvas has an actual width of ZERO.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM