Justin Toth's Blog

Justin is a web developer living in Maryland

Silverlight 3 ChildWindow Modal Popup

clock May 9, 2009 23:18 by author Justin Toth

Tonight I was looking at the Silverlight Toolkit Samples and found the ChildWindow control. This interested me because the first Silverlight tutorial I ever took was ScottGu's on building an application that can search for Digg articles, found here. When you click on an article in the listbox then a modal popup comes up containing more information. He achieved this modal popup functionality by creating a grey rectangle with some opacity that stretched across the screen horizontally and vertically. On top of that was a rounded border control with the content in there that would show up over the rectangle.

I used the concept from ScottGu's tutorial to create a page on my personal website that has a ListBox of some websites that I've created. When you click on one, the modal popup comes up showing more details of the work I did for that website. The ChildWindow control seemed a bit more polished to me so I decided to give it a go.

The first thing you need to do is make sure your project contains a reference to System.Windows.Controls. Interestingly, my application was a Silverlight Navigation project and already contained references to System.Windows.Controls and System.Windows.Controls.Navigation, which made me realize that the navigation functionality that I had been using must be a part of the Silverlight Toolkit! The next thing you'll want to do is add a control to your project for the modal popup. The cool thing here is that there is already a "Silverlight Child Window" control that you can add:

Here's what I did with my child window control, notice how I don't need to worry about the formatting of the window or the modal functionality, all I need to do is add my content that will go inside the modal popup.


<controls:ChildWindow x:Class="TothSolutions.SL.Controls.WorkPopup"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
           Width="600" Height="300"
           Title="WorkPopup">
    <Grid x:Name="LayoutRoot" Margin="10">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
       
        <Image Source="{Binding ThumbNailBig}" Width="250" Margin="0,0,20,0" VerticalAlignment="Center" Grid.Column="0" Grid.Row="0" Grid.RowSpan="3" />
        <TextBlock Text="{Binding Title}" FontSize="20" Foreground="#000" FontWeight="Bold" Grid.Column="1" Grid.Row="0" />
        <HyperlinkButton Content="{Binding HrefLink}" TargetName="_new" NavigateUri="{Binding HrefLink}" FontSize="12" Margin="0,0,0,5" Grid.Column="1" Grid.Row="1" IsTabStop="False" />
        <TextBlock Text="{Binding FullDescription}" FontSize="12" Foreground="#000;" TextWrapping="Wrap" Grid.Column="1" Grid.Row="2" />
        <Button x:Name="CancelButton" Content="Close" Click="CancelButton_Click" Width="75" Height="23" HorizontalAlignment="Center" Margin="0,12,0,0" Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" />
    </Grid>
</controls:ChildWindow>

In the code-behind of the ChildWindow I wait until the control is loaded and then set the title of the modal popup based on the DataContext, which we'll pass in from the parent page. The title as well as a close button will show up at the top of the modal popup without us having to do anything more... Note: you could set the title in the constructor if it's a static value, e.g. this.Title = "My super cool modal popup!";

    public partial class WorkPopup : ChildWindow
    {
        public WorkPopup()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(WorkPopup_Loaded);
        }

        void WorkPopup_Loaded(object sender, RoutedEventArgs e)
        {
            Site site = DataContext as Site;
            this.Title = site.Title;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }

Lastly, on the parent page when the user clicks one of the items in my listbox, I create an instance of my ChildWindow, grab the business object for that row, set that business object as the DataContext of my ChildWindow, and lastly show the ChildWindow.

 private void SiteList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (SiteList.SelectedItem != null)
            {
                Site site = SiteList.SelectedItem as Site;
                WorkPopup popup = new WorkPopup();
                popup.DataContext = site;
                popup.Show();
            }
        }

And that's it, easy peezie... Happy coding!!



Animating Page Transitions in Silverlight 3

clock May 8, 2009 10:31 by author Justin Toth

I wanted to add simple fade out/fade in animations to my page transitions in my Silverlight 3 app. I decided to tackle the fade in animation first, and my first attempt was to add a FadeIn storyboard to the LayoutRoot Grid of each of my pages like so:


        <Grid.Triggers>
            <EventTrigger RoutedEvent="Grid.Loaded">
                <BeginStoryboard>
                    <Storyboard x:Name="FadeIn">
                        <DoubleAnimation Storyboard.TargetName="LayoutRoot" Storyboard.TargetProperty="Opacity"
                From="0" To="1" Duration="0:0:0.300"/>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Grid.Triggers>
 

You also have to add an Opacity of 0 to your LayoutRoot Grid. There are 2 issues with this approach. 1. You can't work in the designer anymore, as it's now set to invisible. 2. There is now duplicate xaml in every page, if you want to make changes to your animations than you'll have to do it in every single page.

My second attempt was to try and fade in/fade out the frame within MainPage.xaml. I set the frame's opacity to 0. The cool thing is that you can add storyboards to be invoked by code rather than attaching them to individual controls:


<UserControl.Resources>
  <Storyboard x:Name="FadeIn">
   <DoubleAnimation Storyboard.TargetName="Frame" Storyboard.TargetProperty="Opacity"
    From="0" To="1" Duration="0:0:0.200"/>
  </Storyboard>
  <Storyboard x:Name="FadeOut">
   <DoubleAnimation Storyboard.TargetName="Frame" Storyboard.TargetProperty="Opacity"
    From="1" To="0" Duration="0:0:0.200"/>
  </Storyboard>
 </UserControl.Resources> 

Next I had some work to do in the code-behind:

        private Button lnkMenu = null;

        public MainPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
            this.FadeOut.Completed += new EventHandler(FadeOut_Completed);
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            FadeIn.Begin();
        }

        private void NavButton_Click(object sender, RoutedEventArgs e)
        {
            lnkMenu = sender as Button;
            FadeOut.Begin();
        }

        void FadeOut_Completed(object sender, EventArgs e)
        {
            String page = lnkMenu.Tag.ToString();
            this.Frame.Navigate(new Uri(page, UriKind.Relative));
            FadeIn.Begin();
        }

Now we have all the page transition animation code in one place so we don't have to deal with the maintainability issue of having it in each page. Also the designers for every single page (including MainPage.xaml) will work now that we're not setting the opacity of any of the pages to 0.



Introduction

clock May 3, 2009 19:30 by author Justin Toth

This is my first post and I'm excited to start blogging!! It seems like every day I run into some sort of issue and once I figure out how to solve it, it makes me wish I had a blog so I could post the solution so that others won't have to spend as much time on it as I did.

This blog will be related to .NET development, and more specifically front-end web development. I used to consider myself an ASP.NET developer but I feel like we've come upon a crossroads. Straight ahead is ASP.NET, to the left is pure javascript development, and to the right is Silverlight. You look further down the ASP.NET road and see that there's another branch up beyond this one, ASP.NET going to the left and ASP.NET MVC going to the right. Which road to take??



About the author

Justin

Justin is a senior developer who has been working with .NET since 2003. His main focus is building highly-interactive web applications using ASP.NET MVC and Dojo or jQuery. Visit his company's site at http://tothsolutions.com.

Page List

    Sign in