How to create modal popup in silverlight ?


Hi
In Silverlight 2.0, creating modal popup was some tedious work. But in Silverlight 3.0,4.0,5.0, we can create very easily like this.

Step1: Create the new silverlight project

step2:Right clik on Solution Exp of silverlight project and add one Silverlight Child Window like this Img

Step3: Keep one Img in ChildWindow.xaml page like this

<controls:ChildWindow x:Class=”ChildWindow.ChildWindow2″

xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation&#8221;

xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml&#8221;

xmlns:controls=”clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls”

Width=”400″ Height=”300″

Title=”ChildWindow2″>

<Grid x:Name=”LayoutRoot” Margin=”2″>

<Grid.RowDefinitions>

<RowDefinition />

<RowDefinition Height=”Auto” />

</Grid.RowDefinitions>

<StackPanel>

<Image x:Name=”img” />

<TextBlock x:Name=”caption” TextWrapping=”Wrap” />

</StackPanel>

<Button x:Name=”CancelButton” Content=”Cancel” Click=”CancelButton_Click” Width=”75″ Height=”23″ HorizontalAlignment=”Right” Margin=”0,12,0,0″ Grid.Row=”1″ />

<Button x:Name=”OKButton” Content=”OK” Click=”OKButton_Click” Width=”75″ Height=”23″ HorizontalAlignment=”Right” Margin=”0,12,79,0″ Grid.Row=”1″ />

</Grid>

</controls:ChildWindow>

Step4:Create one button in main page like this

<Grid>

<Button x:Name=”Button1″ Content=”Click Here” Click=”Button1_Click” Height=”28″ Width=”180″></Button>

</Grid>

Step5: Create the code in code behind page mainpage.xaml.cs like this

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

using System.Windows.Navigation;

using System.Windows.Media.Imaging;

namespace ChildWindow

{

public partial class Test1 : Page

{

public Test1()

{

InitializeComponent();

}

private void Button1_Click(object sender, RoutedEventArgs e)

{

ChildWindow2 win = new ChildWindow2();

win.img.Source = new BitmapImage(new Uri(“Img/Winter.jpg”, UriKind.Relative));

win.Title = “Silverlight popup”;

win.Show();

}

}

}

Advertisements

2 Responses to “How to create modal popup in silverlight ?”

  1. chamara Says:

    hi
    I’m using silverlight with asp.net.i have added silverlight content to asp.net master page using following code

    in the silverlight MainPage.xaml i have a button and when click on the button it opens a ChildWindow.

    private void btnView_Click(object sender, RoutedEventArgs e)
    {
    PopUpWindow PPW = new PopUpWindow();
    PPW.chart1.DataContext = App.Current.Resources[“ChartResult”];

    PPW.Show();
    }
    issue is the child window shows only in masterpage,its not covering the whole screen with the asp.net master page and the content page.how can i make it to show the childWindow on ful screen

  2. andrewgrothe Says:

    simply put and works great. love the built in open/close animations.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: