Page view counter

Sizing in Silverlight – Pixels and Stars

SLLogoWords

Virtually all measurements (width, height, etc.) are measured in Pixels in Silverlight… except when they're not.  

Pixels

When you are creating a shape or setting a margin, or filing in the width or height of a control the implicit unit is pixel.  Thus,

<Button  Content="Button" Width="100" />
<CheckBox  Content="CheckBox" Height="20" Width="75"/>
<StackPanel Orientation="Horizontal" >
    <RadioButton Height="29" Width="106" Content="RadioButton1" />
    <RadioButton Height="29" Width="136" Content="RadioButton2" />
</StackPanel>
<ListBox Width="100" Height="100">
</ListBox>
<Slider Width="250" Height="25"/>
<swc:Calendar Height="150" Width="200" />

All the measurements here are pixels, from the width of the button to the height of the Calendar.

Proportional (Star) Sizing

There are two ways to set the height of a grid's row or the width of a grid's column. One is to use absolute sizing, and thus pixels; the other is to use proportional sizing. When you create a pair of rows by clicking on the margin in Blend, the default is not to create two rows with absolute sizes, but rather two rows whose size is defined relative to one another.  This is indicated in the art board with open locks and in the Xaml with asterisks after the height values.

GridProportionalSpacing

When row and height values are expressed using star sizing, the unit of measure is not pixels; they are proportional values. Thus, Blend has indicated that the top row is to be allocated 25% of the total height and the bottom row 75%

If you click on both locks, you’ll change over to absolute heights, the locks will close, and the Heights will become 120 and 360 (no asterisks) respectively; now measured in pixels, but in the same proportions.

Pop Quiz

One of these things is not like the others:

<Grid.RowDefinitions>
    <RowDefinition Height="0.25*" />
    <RowDefinition Height="0.75*" />
</Grid.RowDefinitions>

<Grid.RowDefinitions>
    <RowDefinition Height="100*" />
    <RowDefinition Height="300*" />
</Grid.RowDefinitions>

<Grid.RowDefinitions>
    <RowDefinition Height="100" />
    <RowDefinition Height="300" />
</Grid.RowDefinitions>

 

-jesse

Published Tuesday, October 21, 2008 6:45 PM by jesseliberty
Filed under: ,

Comments

# re: Sizing in Silverlight – Pixels and Stars

100* <> 100

300* <> 300

Tuesday, October 21, 2008 7:27 PM by yaip

# re: Sizing in Silverlight – Pixels and Stars

I thought Silverlight was like WPF, and the units are logical pixels and not physical pixels.  i.e. it's only 1 pixel on a 96dpi screen.

Tuesday, October 21, 2008 9:41 PM by Joe1972

# re: Sizing in Silverlight – Pixels and Stars

Pop Quiz: Obviously the third one is odd since the heights are constant and the row heights don't adjust when the browser window is resized. The first two are identical since 0.25/0.75 is same as 100/300.

Wednesday, October 22, 2008 12:16 AM by mehroz

# re: Sizing in Silverlight – Pixels and Stars

Hmm, wouldn't the 100* and 300* give something funny? I understand 0.25*, which I read as that this will take up 25% of the width/height. But 300*? This will take up ALOT of space or is it just me?

Another thing about sizing. I have created a game where I have a "GameSurface", which is a Canvas. I have that set to a fixed size (800x600). I use this size in the game to spawn some bubbles inside the screen, so I use GameSurface.Width and GameSurface.Height to make sure my bubbles only spawn inside the actual game area. For fun, I would like the GameSurface to autoscale, so it were dependent on the size of the browser of fullscreen or something like that. So I tried out setting the width and height to "Auto" (can't remember if I tried out the "*" thing). But this gave me problems, and when I wanted to spawn a bubbles it crashed as my width and height were "NaN", so it didn't have a value to use.

Is this the same problem with the "0.25*" way of doing it?

Wednesday, October 22, 2008 3:04 AM by Qbus

# 2008 October 22 - Links for today &laquo; My (almost) Daily Links

Pingback from  2008 October 22 - Links for today &laquo; My (almost) Daily Links

Wednesday, October 22, 2008 3:51 AM by 2008 October 22 - Links for today « My (almost) Daily Links

# re: Sizing in Silverlight – Pixels and Stars

Qbus: The numbers don't matter, it's the ratio between the numbers that matter. Thus you can write:

0.25* & 0.75*

25* & 75*

100* & 300*

231* & 693*

It doesn't matter. The ratio is 1:3 i all cases, which means that the second row will be three times higher than the first line in all cases.

That said, my first two examples are a lot easier to read...

Wednesday, October 22, 2008 4:20 AM by haagel

# re: Sizing in Silverlight – Pixels and Stars

Ok thanks.

But what about my "NaN" problem? I don't have the code here right now, but I just got the thought that this maybe is where I should use the "actualHeight" and "ActualWidth" properties?

Wednesday, October 22, 2008 5:46 AM by Qbus

# Silverlight中的大小控制-像素和比例缩放

Wednesday, October 22, 2008 10:24 AM by allahfan

# Dew Drop - October 22, 2008 | Alvin Ashcraft's Morning Dew

Pingback from  Dew Drop - October 22, 2008 | Alvin Ashcraft's Morning Dew

Wednesday, October 22, 2008 11:26 AM by Dew Drop - October 22, 2008 | Alvin Ashcraft's Morning Dew

# re: Sizing in Silverlight – Pixels and Stars

Now here's an interesting question.  I know that programmatically, you can represent the "Auto" value for a Width or Height by making it equal to Double.NaN.

However, what if you wanted to to represent ".25*" and ".75*"?  I couldn't quite tell from the Silverlight documentation, but I'm assuming that to do this, you would have to use a double value for the Width or Height property using a number that is less than 1.0.  (i.e. Width = 0.25.)

Is this a correct assumption?

Wednesday, October 22, 2008 7:43 PM by JFalcon

# Silverlight news for October 23, 2008

Pingback from  Silverlight news for October 23, 2008

Thursday, October 23, 2008 9:07 AM by Silverlight news for October 23, 2008

# Silverlight Cream for October 22, 2008 -- #404

In this issue: Tim Heuer, Jeff Prosise, Jeff Weber, and Jesse Liberty. From SilverlightCream.com : Silverlight

Thursday, October 23, 2008 3:30 PM by Community Blogs

# Sizing in Silverlight &laquo; Alan&#039;s Topics

Pingback from  Sizing in Silverlight &laquo;  Alan&#039;s Topics

Thursday, July 16, 2009 4:20 PM by Sizing in Silverlight « Alan's Topics