Displays content on top of existing Silverlight content, within the bounds of the Silverlight control.
Namespace:
System.Windows.Controls.Primitives
Assembly:
System.Windows (in System.Windows.dll)
Visual Basic (Declaration)
<ContentPropertyAttribute("Child", True)> _
Public NotInheritable Class Popup _
Inherits FrameworkElement
[ContentPropertyAttribute("Child", true)]
public sealed class Popup : FrameworkElement
XAML Object Element Usage
You typically use the Popup class to temporarily display content that accomplishes a particular task. For example, you might use a popup to display help information when the user moves the mouse pointer over a particular control. The content of the Popup is set using its Child property, and can be any UIElement. Setting the Child property of Popup to a control with an x:Name attribute causes an exception when you create more than one instance of the Popup. This happens because two items with the same name cannot exist in the visual tree.
You would typically create a user control to host the popup content, but this is not required. A popup will always appear within the bounds of the Silverlight plug-in, but on top of the existing content, including other popup controls. Scaling and transforms applied to the parent control of a Popup will be applied to the Popup as well.
Note: |
|---|
In some cases, declaring and adding controls in code to a Popup and making the Popup visible by setting the IsOpen property to true in code, will cause a runtime error. This happens because the children of the Popup are in the visual tree, but the Popup control is not. To avoid a runtime error, add the Popup to the visual tree by declaring it in XAML or by calling LayoutRoot.Children.Add(popupInstance). |
XAML Usage of Popup
As mentioned earlier, typically you create a Popup as part of a user control's logic. However you can create a Popup in XAML. For example, you might define the visual appearance of the Popup as a resource in a XAML ResourceDictionary.
The following example shows how to use a Popup to display a border that contains a text block and a button. The popup content is created inline for conciseness, but you would typically create this content in a separate user control and use an instance of that control as the child of the popup.
' Create the popup object.
Private p As New Popup()
Private Sub showPopup_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
' Create some content to show in the popup. Typically you would
' create a user control.
Dim border As New Border()
border.BorderBrush = New SolidColorBrush(Colors.Black)
border.BorderThickness = New Thickness(5)
Dim panel1 As New StackPanel()
panel1.Background = New SolidColorBrush(Colors.LightGray)
Dim button1 As New Button()
button1.Content = "Close"
button1.Margin = New Thickness(5)
AddHandler button1.Click, AddressOf button1_Click
Dim textblock1 As New TextBlock()
textblock1.Text = "The popup control"
textblock1.Margin = New Thickness(5)
panel1.Children.Add(textblock1)
panel1.Children.Add(button1)
border.Child = panel1
' Set the Child property of Popup to the border
' which contains a stackpanel, textblock and button.
p.Child = border
' Set where the popup will show up on the screen.
p.VerticalOffset = 25
p.HorizontalOffset = 25
' Open the popup.
p.IsOpen = True
End Sub
Private Sub button1_Click(ByVal sender As Object, _
ByVal e As RoutedEventArgs)
' Close the popup.
p.IsOpen = False
End Sub
// Create the popup object.
Popup p = new Popup();
private void showPopup_Click(object sender, RoutedEventArgs e)
{
// Create some content to show in the popup. Typically you would
// create a user control.
Border border = new Border();
border.BorderBrush = new SolidColorBrush(Colors.Black);
border.BorderThickness = new Thickness(5.0);
StackPanel panel1 = new StackPanel();
panel1.Background = new SolidColorBrush(Colors.LightGray);
Button button1 = new Button();
button1.Content = "Close";
button1.Margin = new Thickness(5.0);
button1.Click += new RoutedEventHandler(button1_Click);
TextBlock textblock1 = new TextBlock();
textblock1.Text = "The popup control";
textblock1.Margin = new Thickness(5.0);
panel1.Children.Add(textblock1);
panel1.Children.Add(button1);
border.Child = panel1;
// Set the Child property of Popup to the border
// which contains a stackpanel, textblock and button.
p.Child = border;
// Set where the popup will show up on the screen.
p.VerticalOffset = 25;
p.HorizontalOffset = 25;
// Open the popup.
p.IsOpen = true;
}
void button1_Click(object sender, RoutedEventArgs e)
{
// Close the popup.
p.IsOpen = false;
}
<UserControl x:Class="Popup2.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
FontFamily="Trebuchet MS" FontSize="11"
Width="200" Height="200">
<StackPanel x:Name="LayoutRoot" Background="White">
<Button Width="100" Height="50" x:Name="showPopup"
Click="showPopup_Click" Content="Show Popup" />
</StackPanel>
</UserControl>
System..::.Object
System.Windows..::.DependencyObject
System.Windows..::.UIElement
System.Windows..::.FrameworkElement
System.Windows.Controls.Primitives..::.Popup
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.
Reference