I love using LINQPad to create and test quick snippets. LINQPad brings some of the instant gratification associated with dynamic languages, such as Python and Ruby, to C#. Until recently, I had not used LINQPad to work with UI code.
A few days ago, I was looking to test a small WPF code snippet. I figured there must be a way to use LINQPad. Several searches later, I had a working snippet that I have been using since. The code is quite simple, and it turned out that LINQPad has great built-in support for WPF through the PanelManager.StackWpfElement and PanelManager.DisplayWpfElement API calls. These calls allow you to create UI elements inside a named panel displayed in the lower pane beside the results window. Additional details are available at http://www.linqpad.net/CustomVisualizers.aspx.
Displaying a list box
var items = new ObservableCollection();
// collection initialization
var list = new ListBox();
string template = @"
list.ItemTemplate = template.ToDataTemplate();
list.ItemsSource = items;
DataTemplate is instantiated using an extension method. The method takes a snippet of XAML, plugs it into a standard XAML snippet for data templates (containing standard namespaces), and then instantiates the XAML using XamlReader.Load. You can, of course, change the XAML format to include other custom assemblies.
Code that instantiates Data Template
public static object InstantiateXAML(string xaml)
public static DataTemplate ToDataTemplate(this string template)
string templateFormat = @"
return (DataTemplate) InstantiateXAML(string.Format(templateFormat, template));
The snippet provided in the download link below also demonstrates setting content, attaching event handlers, and obtaining access to dynamically created elements.
You will have to add the following assembly references and namespace imports in LINQPad. The F4 key will cause the dialog that allows these to be added and displayed. Once added, you can save the assemblies as default.
Give it a try. This is the download link: http://www.syncfusion.com/downloads/Support/DirectTrac/92196/wpf1270257153.zip