web 2.0

Fluid Canvas in Silverlight

So you wanted the root canvas to scale proportionately to the host element to achieve fluid layout, and you tried setting Width=”100%” or Height=”50%”, but then horror struck because of this parser error: “Invalid attribute value 100% for property Width/Height”.  Ok, to tell you the truth you cannot set “%” to the Width and Height properties because the % is not accepted. Read on for the solution.

The BrowserHost instance has the Resize() event which fires every time the host element is resized and the ActualWidth/ActualHeight properties which tells the dimension of the host element. So, to automatically resize the root canvas, handles the BrowserHost.Resize() event, and from there you can resize the canvas. Let’s sum it up with VB.NET and C# code:

VB.NET – Page.xaml.vb

Imports System.Windows.Interop
Partial Public Class Page
    Inherits Canvas
    ' Height and width of the canvas in percentage
    Private _widthCanvas As Double = 100.0#
    Private _heightCanvas As Double = 100.0#
    Public Sub Page_Loaded(ByVal o As Object, ByVal e As EventArgs)
        InitializeComponent()
        AddHandler BrowserHost.Resize, AddressOf BrowserHost_Resize
    End Sub
    Public Sub BrowserHost_Resize(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.Width = CDbl(BrowserHost.ActualWidth) * _widthCanvas / 100.0#
        Me.Height = CDbl(BrowserHost.ActualHeight) * _heightCanvas / 100.0#
    End Sub
End Class

C# - Page.xaml.cs

public partial class Page : Canvas
{
     // Height and width of the canvas in percentage
     private double _widthCanvas = 100.0;
     private double _heightCanvas = 100.0;

     public void Page_Loaded(object o, EventArgs e)
     {
         InitializeComponent();
         BrowserHost.Resize += BrowserHost_Resize;
     }

     public void BrowserHost_Resize(object sender, System.EventArgs e)
     {
         this.Width = (double)BrowserHost.ActualWidth * _widthCanvas / 100.0;
         this.Height = (double)BrowserHost.ActualHeight * _heightCanvas / 100.0;
     }
}

 

The root canvas now automatically resizes according to _widthCanvas and _heightCanvas value. Anyway, I believe some of you folks might be now saying “I want to scale the root canvas proportionally to width and height of web-page body.” If you said so, you’ve need to instead change the size of the host element. You can use CSS for that purpose. See my example below. Highlighted is where you can change the size of the host element.

 

 

Online Demo - Source Code (31.20 kb)

Tags:

Silverlight 1.1 | Tutorial

Comments

cticoder.wordpress.com , on 10/19/2008 11:45:02 AM Said:

pingback

Pingback from cticoder.wordpress.com

Setting a Silverlight canvas to full width/height of the browser in Silverlight 2 « CTICoder

Busby SEO Test , on 12/7/2008 5:10:14 PM Said:

Busby SEO Test

thanks for sharing your ideas., i really appreciate it!!

chinhdo.com , on 2/12/2009 10:36:38 AM Said:

pingback

Pingback from chinhdo.com

Bag of Links #2 » Chinh Do

Hits Tech Gadgets , on 3/15/2009 6:35:42 PM Said:

Hits Tech Gadgets

its make the silverlight application so good

Alyssa , on 4/6/2009 8:24:01 AM Said:

Alyssa

To constrain the proportion, why don't just remove _heightCanvas and use single variable?

this.Width = (double)BrowserHost.ActualWidth * _widthCanvas / 100.0;
this.Height = (double)BrowserHost.ActualHeight * _widthCanvas / 100.0;

mliebster , on 4/30/2009 3:41:05 AM Said:

mliebster

I was trying to create my own customized canvas and am running into problems.

I posted the issue here: silverlight.net/forums/p/93169/214175.aspx#214175

The display aspect is working fine, but any objects within the canvas are null in the codebehind.

Any assistance would be appreciated.

Thanks!

запознанства , on 7/15/2009 11:29:40 PM Said:

запознанства

thanks for the code Smile

autocad , on 7/15/2009 11:30:38 PM Said:

autocad

Good Silverlight info.

free ebook , on 7/20/2009 7:11:07 AM Said:

free ebook

I recently came across your blog and have been reading along. I thought I would leave my first comment. I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.

jammer , on 7/28/2009 9:12:44 AM Said:

jammer

need more of u

moving , on 7/28/2009 9:12:57 AM Said:

moving

do it right now

atlanta movers , on 7/28/2009 9:13:15 AM Said:

atlanta movers

cant make it

jewish democratic , on 8/5/2009 6:12:28 AM Said:

jewish democratic

we love u all

portland movers , on 8/5/2009 6:13:19 AM Said:

portland movers

how far can we go there

casinos online , on 8/8/2009 1:30:17 AM Said:

casinos online

I am hoping the same best work from you in the future as well. In fact your creative writing abilities has inspired me.Really the blogging is spreading its wings rapidly...

Halan , on 8/20/2009 8:02:18 PM Said:

Halan

Do you have any example of it? Thanks.

jewish singles , on 8/26/2009 3:48:36 AM Said:

jewish singles

Great post! I’m just starting out in community management/marketing media and trying to learn how to do it well - resources like this article are incredibly helpful. As our company is based in the US, it’s all a bit new to us. The example above is something that I worry about as well, how to show your own genuine enthusiasm and share the fact that your product is useful in that case..

Daniel , on 8/28/2009 2:01:31 AM Said:

Daniel

I this applicable in silverlight2 as well

green tea weight loss , on 9/6/2009 8:17:47 AM Said:

green tea weight loss

the coding is great and it has solved a lot of problems for me.

hp laptops , on 9/8/2009 10:29:03 PM Said:

hp laptops

What is 'Silverlight'? I have downloaded it from MSN, but don't know what are its usage and how it is used.?

Shade , on 9/9/2009 6:44:33 AM Said:

Shade

nice coding, thank you for sharing! i tried it like 10 times and there was always this error

Priceline Help , on 9/10/2009 3:37:01 PM Said:

Priceline Help

A fluid canvas will make my homepage looks great in any browser. I thought we can't do anything since the percentage of the height can't be change. But your tips really works

best bathroom fixtures , on 9/12/2009 2:14:26 AM Said:

best bathroom fixtures

This will save the time and money that we usually use to hire a designer and fix the width

Istria , on 9/12/2009 2:17:05 AM Said:

Istria

Silverlight can replace some web design application in the future. I hope this fluid canvas tips can work for a mash up too

http://www.mejorescasinobonos.com , on 9/24/2009 1:06:06 AM Said:

http://www.mejorescasinobonos.com

You made me late for work reading this. Nice blog, just bookmarked it to come back later.

drug rehab centers dallas , on 9/30/2009 12:03:53 PM Said:

drug rehab centers dallas

I wonder if they have released a new version of silverlight. Maybe there will be some features to make it automated

payday , on 10/1/2009 7:23:27 AM Said:

payday

I like your writing style)

payday , on 10/1/2009 7:23:52 AM Said:

payday

great, thanks for the script!

payday , on 10/1/2009 7:24:17 AM Said:

payday

Many thanks for sharing

payday , on 10/1/2009 7:24:35 AM Said:

payday

Thanks for the useful info!

payday , on 10/1/2009 7:24:53 AM Said:

payday

Everyone just remember - Givers Gain the more you give the more you Get!

XRF , on 10/7/2009 7:39:53 PM Said:

XRF

HI,
  How to know internet usage even after deleting cookies or other private data from internet options?

SEO , on 10/8/2009 6:40:04 PM Said:

SEO

@XRF: check at your ISP's website

microgaming online casino , on 10/12/2009 10:19:24 PM Said:

microgaming online casino

your site rocks with examples

Free Service Manual , on 10/13/2009 8:33:06 AM Said:

Free Service Manual

I thought I would leave my first comment. I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.

Fast money loan , on 10/13/2009 6:15:55 PM Said:

Fast money loan

Nice post . keep up the good work

working less , on 10/14/2009 7:56:24 AM Said:

working  less


Very good and interesting site Smile

Antivirus software , on 10/19/2009 10:34:03 PM Said:

Antivirus software

Hi,

What type of antivirus should I get on a Mac running XP with Bootcamp?

online gambling , on 10/20/2009 6:52:48 PM Said:

online gambling

I am trying to learn how to create website, so my task as of now is to gathered resources and codes of different application that I can used to learn the application. I would say i am gathering a lot about silverlight. Thank you for sharing this codes it will help me alot.

Add comment


(Will show your Gravatar icon)

biuquote
  • Comment
  • Preview
Loading