WPF Example

This is a simple introduction to Windows Presentation Foundation, the new GUI library that is part of .NET 3.0.

This code was translated from an IronRuby sample by Steve Gilham.

The WPF library is in the System.Windows namespace.

When you run this example, it will look like:



import clr clr.AddReferenceByName("PresentationFramework, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35") clr.AddReferenceByName("PresentationCore, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35") import System.Windows
 * 1) Reference the WPF assemblies

Window = System.Windows.Window Application = System.Windows.Application Button = System.Windows.Controls.Button StackPanel = System.Windows.Controls.StackPanel Label = System.Windows.Controls.Label Thickness = System.Windows.Thickness DropShadowBitmapEffect = System.Windows.Media.Effects.DropShadowBitmapEffect
 * 1) Initialization Constants

my_window = Window my_window.Title = 'Welcome to IronPython'
 * 1) Create window

my_stack = StackPanel my_stack.Margin = Thickness(15) my_window.Content = my_stack
 * 1) Create StackPanel to Layout UI elements

my_button = Button my_button.Content = 'Push Me' my_button.FontSize = 24 my_button.BitmapEffect = DropShadowBitmapEffect
 * 1) Create Button and add a Button Click event handler

def clicker(sender, args):

# Create new label my_message = Label my_message.FontSize = 48 my_message.Content = 'Welcome to IronPython!'

# Add label into stack panel of controls my_stack.Children.Add (my_message)

my_button.Click += clicker

my_stack.Children.Add (my_button)

my_app = Application my_app.Run (my_window)
 * 1) Run application

Back to Contents.