This article has a corresponding video that can be viewed here.
Are you new to WPF? This walk though will enable you to have a successful first time experience with the WPF Designer and creating your first WPF application. You will learn to easily create, move and align controls, create data bindings and edit control collections using the powerful features of the WPF Designer for Visual Studio 2010. We will complete the exercise by developing a Survey form.
Requirements
View the Setting up Visual Studio for WPF and Silverlight Development topic.
The first four short sections of this walk though introduce you to using the WPF Designer for creating, moving, aligning and sizing controls.
To create your first WPF Application Project
Figure 1 New Project Menu Selection
Figure 2 New Project Dialog
The Target Framework Version ComboBox value defaults to the last selection made. When creating a new project, verify the desired Target Framework Version is selected.
While it is permissible to have spaces in the project name this practice discouraged.
Let's give our application a proper title. The Title property is displayed in the top left side of the window chrome.
Each control has a default property that is automatically selected in the Properties Window when that control is selected.
Figure 3 Properties Window
Figure 4 WPF Designer and XAML Editor
Controls can be added to an application using the ToolBox or by editing the XAML text directly in the XAML Editor.
Figure 5 Create Control
The WPF Designer displays control adorners on the design surface to assist developer when using the mouse to move or resize a control. Adorners also supply visual feedback about control alignment and other control specific information.
When controls are being moved or resized, the WPF Designer displays various types of snaplines to assist the developer in positioning a control on the design surface relative to other controls.
Figure 6 Alignment Adorners
The Margin property is of type Thickness. The property values are read left, top, right and bottom. A Thickness can also be represented with a single value or with two values. If a single value is supplied, all four sides are equal. If two values are supplied they are read (left and right), (top and bottom).
Figure 7 Alignment and Resize Adorners
Figure 8 Edge Snaplines (EdgeSnapLines.png)
To turn off Snaplines when moving a control, hold the ALT key down while dragging the control around on the design surface.
Use arrow keys to nudge a control, pixel by pixel into perfect position!
Figure 9 Control Snaplines
Figure 10 Text Baseline Snaplines
At this time delete any buttons or other controls you have placed on the design surface by selecting the control and pressing the DELETE key to delete the control.
Please save your application by pressing CTRL+S or by clicking the Save icon on the ToolBar.
The rest of this walk through will focus on creating this Simple Survey application. You will learn many new features of the WPF Designer while building this application.
Figure 11 Completed Application
The search feature of the Properties Window filters the properties and can save you lots of time during application development. Very handy.
Figure 12 Form Title - TextBlock Text
Figure 13 Form Title - TextBlock positioning
In Figures 12 and 13, notice the preview of the TextBlock in the Preview Icon displayed to the left of the control name. This same Preview Icon is also displayed in the Document Outline when the mouse is hovered over controls listed there.
It's time to give our user interface (UI) some controls the user can interact with at run-time.
Figure 14 Form Controls
Figure 15 Label Content Property
The Label control uses the Content property to display its text. You may be thinking why is the text for the Label in the Content property and the text for the TextBlock in the Text property? This is because the two properties are different data types with different associated features.
The Label Content property can contain just about anything, text, image, grid, etc.
The Label control can associate an Access Key with another control, so that when the Access Key is pressed, focus will be moved to the target control. The Label Content property plays a role in this.
The TextBlock Text property is special. It can contain text or inline content flow elements. See http://msdn.microsoft.com/en-us/library/bb613554.aspx for more information on inline content. Inline content allows you to have multi-line formatted content in a TextBlock.
Figure 16 Binding Target Property
Figure 17 Data Binding Builder
You have just data bound labels to their associated TextBox to enable Access Key control selection. Data binding in WPF is one of the most powerful features of the platform and is used extensively in all WPF applications. For example, binding UI controls to database or collection data, or intra-form binding that provides an interactive experience for users of your application. You will find that data binding in the UI will save you from writing a lot of code you would normally write if developing on another platform.
A proper understand of WPF Data Binding is essential for the WPF developer. MSDN documentation provides clear and concise information that you can read here http://msdn.microsoft.com/en-us/library/ms752347.aspx.
Figure 18 ComboBox Items Collection
Figure 19 Collection Editor
In this section we will use a GroupBox to display RadioButtons. RadioButtons are mutually exclusive; meaning that when one is selected the previously selected RadioButton is deselected automatically.
We will also introduce a new panel control, the StackPanel. StackPanels stack their child controls either horizontally or vertically based on their Orientation property.
Figure 20 GroupBox
Figure 21 Parenting Control on Creation
You can also drag controls from the ToolBox to the XAML Editor. When a control is dragged to the XAML editor, no properties are given default values.
Figure 22 StackPanel as a child of the GroupBox
Figure 23 Resetting Property Values
Figure 24 Auto Sized StackPanel
Figure 25 Multi-Selection Control Editing
You can also multi-select all controls in a container by selecting the container and pressing CTRL+A. In figure 25 above, select the StackPanel and press CTRL+A to select the three RadioButtons.
Figure 26 Resizing GroupBox
Figure 27 RadioButton Content
Figure 28 ComboBox Selected Index
To complete our application we need to add two Buttons and add some code that will execute when the Button is clicked.
Figure 29 Properties Window Events Tab
Visual Basic Code
Class FirstWPFApplication Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs ) Handles btnCancel.Click Me.Close() End Sub Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs ) Handles btnOK.Click MessageBox.Show("Thank you for your feedback") Me.Close() End Sub End Class
C# Code
namespace FirstWPFApplication { /// <summary> /// Interaction logic for Window1.xaml /// </summary> public partial class Window1 : Window { public Window1() { InitializeComponent(); } private void btnOK_Click(object sender, RoutedEventArgs e) { MessageBox.Show("Thank you for your feedback."); this.Close(); } private void btnCancel_Click(object sender, RoutedEventArgs e) { this.Close(); } } }
Figure 30 Running the Application
Microsoft values your opinion about our products and documentation. In addition to your general feedback it is very helpful to understand:
Thank you for your feedback and have a great day,
Karl Shifflett Visual Studio Cider Team
Hi,
I'm a beginner. Your Blog is very useful for me to learn the WPF Concepts. Thanks.
P.S:
In Setting the Target Property for the Access Key section, i got stuck up with the description select textbox control. But i've done by seeing the picture[Selecting the label control for that binding].
sharanya kannan,
Thank you for your valuable feedback. I'll update the wording.
Have a great day,
Karl