Silverlight MasterPages

Posted on April 1, 2008. Filed under: Silverlight |

Someone in silverlight forums was looking for asp.net master page kind of functionality. Here is one way to do it

<Grid x:Name=”LayoutRoot” Background=”White”>
        <Grid.RowDefinitions>
            <RowDefinition Height=”50″></RowDefinition>
            <RowDefinition Height=”*”></RowDefinition>
            <RowDefinition Height=”50″></RowDefinition>           
        </Grid.RowDefinitions>
        <StackPanel   Height=”50″ Grid.Row=”0″  Orientation=”Horizontal”>
            <Button  Click=”Button_Click” Height=”25″ Tag=”c1″ Content=”Content page 1″></Button>
            <Button Click=”Button_Click” Height=”25″ Tag=”c2″  Margin=”10,0,0,0″ Content=”Content page 2″></Button>
            <Button  Click=”Button_Click” Height=”25″ Tag=”c3″ Margin=”10,0,0,0″ Content=”Content page 3″></Button>
        </StackPanel>
        <Grid Grid.Row=”1″ x:Name=”ContentArea”>
            <ContentControl x:Name=”Container”></ContentControl>
        </Grid>
        <Canvas Grid.Row=”2″ Background=”MediumSlateBlue”>
            <TextBlock Text=”This is footer” Margin=”100,5,100,5″></TextBlock>
        </Canvas>
    </Grid>

we define  a grid with 3 rows the center row will take up all the space between the header row and footer row(top and bottom), when we need to change the content we are going to load the content into the ContentControl in Row 1 of the grid

 private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button b = sender as Button;
            string loadContent = b.Tag.ToString();
            switch (loadContent)
            {
                case “c1″:
                    this.Container.Content = new ContentPage1();
                    break;
                case “c2″:
                    this.Container.Content = new ContentPage2();
                    break;
                case “c3″:
                    this.Container.Content = new ContentPage3();
                    break;
            }
        }

Make a Comment

Make A Comment: ( 5 so far )

blockquote and a tags work here.

5 Responses to “Silverlight MasterPages”

RSS Feed for Lee’s corner Comments RSS Feed

[...] Rob Houweling produced a very nice Motion Blur demo … no source (yet)… but it looks good! Silverlight MasterPages Lee continues with a blog post proposing a way to do SL2 MasterPages. I’m having trouble copying [...]

Hi, nice one.. it would be great if you can add a few animations…

good idea

AN ERROR OCCOURS

Error 1 The type or namespace name ‘ContentPage1′ could not be found (are you missing a using directive or an assembly reference?) D:\MasterSilverLight\MasterSilverLight\Page.xaml.cs 28 50 MasterSilverLight

Hi,
In the code behind it creates a new instance of the a UserControl called Content1,Content2,Content3.
looks like your project doesnt have UserControls with those names


Where's The Comment Form?

Liked it here?
Why not try sites on the blogroll...