September 23, 2010

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!



'Like' us on our FaceBook page if you find this blog useful. Thanks!


Did you like this post?
kick it on DotNetKicks.com Save on Delicious
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP 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 has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

0 Responses to "Programmatically add Content to the Popup control in Silverlight"
 

Copyright © 2009-2012 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions