SIGN UP MEMBER LOGIN:    
ARTICLE

Popup Control in Silverlight 2.0

Posted by Dipal Choksi Articles | Silverlight with C# January 23, 2009
In this article we will walk through a basic sample for using the Popup control in Silverlight 2.0.
Reader Level:

In this article we will walk through a basic sample for using the Popup control in Silverlight 2.0.

 

Typically the popup control is used to display some temporary and additive information, optionally allowing the user to provide a short response.

A popup is displayed within the bounds of the Silverlight plug-in and is the top level element - shown on top of the existing content. Note that the transforms applied to the Parent control of the Popup will be applied to the Popup control too.

 

Some scenarios where using popups would be a good choice are

  • Confirmation message box
  • Detail view of row data.
  • Quick Selections

Options for creating and using Popups

 

Option 1 - Add controls to the Popup object using XAML

Option 2 - Add controls to the Popup object using Code

Option 3 - Design a separate user control which will be included as the content of the Popup control.

 

In the sample we will create a new Silverlight user control. The user control will contain the controls and layout to be rendered for the Popup. This provides a good separation of logic and offers an opportunity of re-usability.

 

User control : UCApproveConfirm.xaml

 

<UserControl x:Class="SilverlightApplication1.ApproveConfirm"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Width="400" Height="100">

<Border BorderThickness="5" BorderBrush="Black">

<Grid x:Name="ucGrid" Background="Beige">

<Grid.RowDefinitions>

<RowDefinition/>

<RowDefinition/>

</Grid.RowDefinitions>

<Grid.ColumnDefinitions>

<ColumnDefinition/>

<ColumnDefinition/>

</Grid.ColumnDefinitions>

<TextBlock Margin="5"

Text="Are you sure you want to approve all the Incidents?" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2"/>

<Button Width=" 60" Height="30" Content="Yes" Click="Button1_Click" Grid.Column="0" Grid.Row="1"/>

<Button Width=" 60" Height="30" Content="No" Click="Button2_Click" Grid.Column="1" Grid.Row="1"/>

</Grid>

</Border>

</UserControl>

In the code behind for the user control we would add code to handle the user selection.

 

In this example, we will only add some code to "close" the Popup.

 

private void Button1_Click(object sender, RoutedEventArgs e)

{

this.Visibility = Visibility.Collapsed;

}

Now we make use of the User control to display the popup. The popup will be displayed when the "Approve All" button is clicked.

 

We first import the System.Windows.Controls.Primitives namespace.

 

In the Page code:

 

using System.Windows.Controls.Primitives;

 

In this basic sample, we will just add a button, which on being clicked, will display the user control within a popup.

 

In the Page xaml:

 

<StackPanel x:Name="mainPanel">

<Button x:Name="btnPopup" Width="100" Click="btnApproveAll_Click"

Content="Approve All"></Button>

</StackPanel>

 

To display the Popup, we create a Popup object and set the User control as the Child of the Popup. We also add a small horizontal and vertical offset to improve the visibility/layout. As per Microsoft documentation: "To avoid a runtime error, add the Popup to the visual tree by declaring it in XAML or by calling LayoutRoot.Children.Add(popupInstance)." 

 

In the Page code:

 

Popup popupConfirmation;

 

private void btnApproveAll_Click(object sender, RoutedEventArgs e)

{

popupConfirmation = new Popup();

popupConfirmation.Child = new ApproveConfirm();

this.mainPanel.Children.Add(popupConfirmation);

popupConfirmation.HorizontalOffset = 25;

popupConfirmation.VerticalOffset = 25;

popupConfirmation.IsOpen = true;

}

 

Image: The Popup confirmation box in action.

 

Note: You cannot nest a Popup control within another Popup.

 

Conclusion: In this article we demonstrated the use of the Popup control in Silverlight. It provides significant user interaction in a very intuitive way.

 

Happy Coding!

Login to add your contents and source code to this article
share this article :
post comment
 

Should add that Popup is in namespace System.Windows.Controls.Primitives

Posted by Mad Bob Mar 22, 2011

Hi. it is a nice post. But "this.Visibility = Visibility.Collapsed;" this code doesn't close the form. I want to close. Do you have any suggestion?

Posted by Foyzul karim Dec 12, 2010
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor