Home Course Index << Prev. Next >> PDF Version of this Page

Course API Comparison
Chapter C3: A WPF Project completely written in C#


Copyright © by V. Miszalok, last update: 21-09-2010


For an introduction into Microsofts WPF-API see:

WPF

Comparison of Microsofts Windows Forms API with WPF:

Compare Windows Forms and WPF

Preliminaries

Guidance for Visual C# 2010 Express Edition:

1) Main Menu after start of VS 2010: Tools → Options → check lower left checkbox: Show all Settings → Projects and Solutions → Visual Studio projects location: → C:\temp

2) Main Menu after start of VS 2008: File → New Project... → Visual Studio installed templates: Empty Project →
Name: compareWPF_Cs
→ OK

3) Main Menu after start of VS 2010: Project... → compareWPF_Cs Properties → Application →
Output type:
switch from Console Application to Windows Application

4) In the Solution Explorer-window right click the branch References. Add Reference → .NET-tab scroll down until you find Presentation Core. Click OK. Check whether Presentation Core arrived in the branch References. In the same way you have to add 3 more references: Presentation Framework, System and WindowsBase.

5) In the Solution Explorer-window right click the main branch compareWPF_Cs. Choose Add → New Item → Code File → Name: compareWPF_Cs.cs
 
 

compareWPF_Cs.cs

Write the following lines into the empty window of compareWPF_Cs.cs:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
class WPF_Cs : Application
{ [STAThread] public static void Main()
  { Window     window     = new Window();
    Viewbox    viewbox    = new Viewbox();
    Border     border     = new Border();
    StackPanel stackPanel = new StackPanel();
    Button     button1    = new Button();
    Button     button2    = new Button();
    window.Title    = "WPF Windows Program in C#";
    window.Width    = 500;
    window.Height   =  80;
    window.Content  = viewbox;
    window.Show();
    viewbox.Child   = border;
    border .Child   = stackPanel;
    border.BorderBrush     = new SolidColorBrush( Colors.Black );
    border.BorderThickness = new Thickness( 2 );
    stackPanel.Orientation = Orientation.Horizontal;
    stackPanel.Margin      = new Thickness( 2 );
    stackPanel.Children.Add( button1 );
    stackPanel.Children.Add( textBox );
    stackPanel.Children.Add( button2 );
    button1.Content = "Talk!";
    button2.Content = "Clear";
    button1.Click += new RoutedEventHandler( button1Click ); 
    button2.Click += new RoutedEventHandler( button2Click );
    textBox.Margin        = new Thickness( 2, 0, 2, 0 );
    textBox.TextAlignment = TextAlignment.Center;
    textBox.MinWidth      = 300;
    new Application().Run();
  }
  static TextBox textBox = new TextBox();
  static void button1Click ( Object sender, EventArgs e )
  { textBox.Text = "WPF Windows Program in C#. Resize!";
  }
  static void button2Click ( Object sender, EventArgs e )
  { textBox.Text = "";
  }
}

Click Debug → Start Without Debugging Ctrl F5.
Click the Talk!-button and resize the window.

top of page: