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!



Will you give this article a +1 ? Thanks in advance


About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

No comments: