Posts tagged ‘Sliverlight’

Flex Anchored Layout in Silverlight

When I started using Silverlight, I was moving over from Flex, and the first thing I noticed was: Where is my anchored positioning?

In some cases using a table (Grid) for a layout is not ideal, and if your moving over from Flex like I did, you may want to have something you’re familiar with. This post walks though how to simulate anchored layout in Sliverlight, using a single cell and applying alignment and margin properties to its elements.

The following example, built in Silverlight, illustrates the types of anchored positioning:

Example screen normal size

Example screen normal size

Example screen stretched

Example screen stretched

Here is the XAML for the layout of the screen above.

XAML for screen

XAML for screen

One Cell, Many Possibilities

As you can see from the screenshots above, as the page is stretched, the elements stretch and retain their positions also. Normally this type of layout in Silverlight would require a Grid with 3 columns and 3 rows, and you would have to add the definitions for them as well as set the column and row properties for each element inside the Grid. To simplify how the page is layed out, only a single cell is needed, and instead the HorizonalAlignment, VerticalAlignment, and Margins are adjusted. A nice side effect of this type of layout, is the XAML used is significantly reduced. I’ll go through each element in turn and explain how it’s layout is simulated from Flex. On the left side of each explanation is the constraints as would be defined in Flex.

Header

Flex layout constraints for Header

Flex layout constraints for Header

     

The header of the page stays at the top and stretches left and right. In Flex a top, left, and right position for the Header would have been set. In Silverlight, the header of the page is anchored to the top using a VerticalAlignment of Top, and then stretches both left and right by using a HorizontalAlignment of Stretch. A fixed Height is set, and a left Margin of 100 is applied.


<Border Background=”Red” Height=”50″ VerticalAlignment=”Top” HorizontalAlignment=”Stretch” Margin=”100,0,0,0″>
<TextBlock Text=”Header” Style=”{StaticResource Number}” />
</Border>

Navigation

Flex layout constraints for Navigation

Flex layout constraints for Navigation

     

The navigation pane on the page stays vertically centered on the left. In Flex, the verticalCenter and left positions would be set for the Navigation. In Silverlight, VerticalAlignment is set to Stretch, and HorizontalAlignment is set to Left, with a fixed Height applied to the element.



<Border Background=”Green” Width=”80″ Height=”200″ VerticalAlignment=”Stretch” HorizontalAlignment=”Left”>
<TextBlock Text=”Navigation” Style=”{StaticResource Number}” />
</Border>

Body

Body layout constraints in Flex

Body layout constraints in Flex

     

The body of the page stretches in all directions while still leaving room for the other elements. In Flex, top, left, right, and bottom positions would be set. In Silverlight, a VerticalAlignment and HorizontalAlignment of Stretch is set, as well as Margins on all sides.



<Border Background=”Orange” VerticalAlignment=”Stretch” HorizontalAlignment=”Stretch” Margin=”100,80,80,80″>
<TextBlock Text=”Body” Style=”{StaticResource Number}” />
</Border>

Buttons

Flex layout constraints for Buttons

Flex layout constraints for Buttons

     

The buttons on the page are centered horizontally on the bottom. In Flex, the positions of bottom and right would be set. In Silverlight, VerticalAlignment is set to Bottom, and HorizonalAlignment is set to Center with a fixed Width and Height on the element.



<Border Background=”Blue” Width=”200″ Height=”60″ VerticalAlignment=”Bottom” HorizontalAlignment=”Stretch”>
<TextBlock Text=”Buttons” Style=”{StaticResource Number}” />
</Border>

Page

Flex layout constraints for Page

Flex layout constraints for Page

     

The page number is in the bottom right of the page. In Flex, a position on the bottom and right would be set. In Silverlight, VerticalAlignment is set to Bottom, and HorizontalAlignment is set to Right, with a fixed Width and Height applied to the element.



<Border Background=”Purple” Width=”80″ Height=”60″ VerticalAlignment=”Bottom” HorizontalAlignment=”Right”>
<TextBlock Text=”Page” Style=”{StaticResource Number}” />
</Border>

Summary

The use of the Grid layout can be simplified and simulate Flex anchored layout by only using a single cell, and applying alignments with margins. Standard absolution positioning can also be achieved within a single cell grid by applying a VerticalAlignment of Top, a HorizontalAlignment of Left, and then using left and top margins for x and y respectively. The translation from Flex anchored layout to Silverlight can be summarized by the table below:

 Description 

 Flex 

 Silverlight 

Top left position top, left VerticalAlignment=Top, HorizontalAlignment=Left
Bottom right position bottom, right VerticalAlignment=Bottom, HorizontalAlignment=Right
Center horizontally center HorizontalAlignment=Center
Center vertically verticalCenter VerticalAlignment=Center
Stretch horizontally left, right HorizontalAlignment=Stretch
Stretch vertically top, bottom VerticalAlignment=Stretch