Programmatically add Content to the Popup control in Silverlight

Adding a Popup to a Silverlight application is as easy as dragging and dropping a Popup control from the ToolBox

<Popup x:Name="popup" />

However if you want to programmatically add content to the Popup control, here’s how to do so:

private void Button_Click(object sender, RoutedEventArgs e)
{
//Create Popup content with a border, background color
// and a child element Button
popup.Child = new Border()
{
Height= 150,
Width=150,
Background = new SolidColorBrush(Colors.LightGray),
Child = new Button() { Content = "OK", Height=50, Width=50 }
};

// Show/hide Popup
popup.IsOpen = !popup.IsOpen;
}
}

As you can see, we are programmatically adding a Border element, specifying attributes of the Popup like Height, Width, Background Color and also adding a Button inside the popup, with attributes of its own. It’s common for developers to use the Popup type to create Cascading Menus programmatically!






About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

No comments: