Silverlight MasterPages

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;
            }
        }

7 thoughts on “Silverlight MasterPages

  1. 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

    1. 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

  2. Thanks Lee.
    Your example has really given me an insight that all other tutorials so far seems to have failed at.

Leave a comment