25
Module 3 Designing and Developing a User Interface

Module 3 Designing and Developing a User Interface

Embed Size (px)

Citation preview

Page 1: Module 3 Designing and Developing a User Interface

Module 3

Designing and Developing

a User Interface

Page 2: Module 3 Designing and Developing a User Interface

Module Overview

• Defining Page Layout

• Using Content Controls

• Using Items Controls

• Sharing Logical Resources in a Window

Page 3: Module 3 Designing and Developing a User Interface

Lesson 1: Defining Page Layout

• WPF Page Layout Model

• WPF Layout Classes

• Demonstration: Defining Layout by Using Panels

• Demonstration: Defining Layout by Using Grids

Page 4: Module 3 Designing and Developing a User Interface

WPF Page Layout Model

Measurement passMeasurement pass11Hello World!

Evaluate each member of the Children collection to determine its DesiredSizeEvaluate each member of the Children collection to determine its DesiredSize

•Abstract rectangular bounding box

•Retrieve by calling GetLayout on a FrameworkElement

•Abstract rectangular bounding box

•Retrieve by calling GetLayout on a FrameworkElement

Arrangement passArrangement pass22 Window or Page element

Determine the final size of each child object and place in its layout slotDetermine the final size of each child object and place in its layout slot

Layout class

Child objects

Page 5: Module 3 Designing and Developing a User Interface

WPF Layout Classes

CanvasCanvas

VirtualizingStackPanelVirtualizingStackPanel

DockPanelDockPanel

GridGrid

StackPanelStackPanel

Wrap PanelWrap Panel

Panel

Background

Children

ZIndex

Panel

Background

Children

ZIndex

Page 6: Module 3 Designing and Developing a User Interface

Demonstration: Defining Layout by Using Panels

In this demonstration, you will see how to:

•Use the Canvas class

•Use the StackPanel class

•Use the WrapPanel class

•Use the DockPanel class

Page 7: Module 3 Designing and Developing a User Interface

Demonstration: Defining Layout by Using Grids

In this demonstration, you will see how to use the Grid class

Page 8: Module 3 Designing and Developing a User Interface

Lesson 2: Using Content Controls

• Understanding Content Controls

• Demonstration: Creating a User Interface by Using Content Controls

• Understanding Headered Content Controls

• Demonstration: Creating a User Interface by Using Headered Content Controls

Page 9: Module 3 Designing and Developing a User Interface

Understanding Content Controls

Common content controls:

• Button

• CheckBox

• GroupItem

• Label

• RadioButton

• RepeatButton

• ToggleButton

• ToolTip

Common content controls:

• Button

• CheckBox

• GroupItem

• Label

• RadioButton

• RepeatButton

• ToggleButton

• ToolTip

•Contains a single item

•Has a Content property

•Contains a single item

•Has a Content property

ExamplesExamples TextText

DateTimeDateTime

UIElementUIElement

Multiple objects

Multiple objects

This is text content of a Button

04/03/2007 13:06

Button

Page 10: Module 3 Designing and Developing a User Interface

Demonstration: Creating a User Interface by UsingContent Controls

In this demonstration, you will see how to use the ContentControl class

Page 11: Module 3 Designing and Developing a User Interface

Understanding Headered Content Controls

Headered content controls:

• Expander

• GroupBox

• TabItem

Headered content controls:

• Expander

• GroupBox

• TabItem

•Specialized ContentControl

•Has a Content property

•Has a Header property

•Specialized ContentControl

•Has a Content property

•Has a Header property

•Example•Example

TabItem

header

TabItem

header

GroupBoxGroupBox

ExpanderExpander

Page 12: Module 3 Designing and Developing a User Interface

Demonstration: Creating a User Interface by Using Headered Content Controls

In this demonstration, you will see how to:

•Use the TabItem class

•Use the GroupBox class

•Use the Expander class

Page 13: Module 3 Designing and Developing a User Interface

Lesson 3: Using Items Controls

• Understanding Items Controls

• Handling Item Selection

• Demonstration: Creating a User Interface by Using Items Controls

Page 14: Module 3 Designing and Developing a User Interface

Understanding Items Controls

• Contains multiple objects

• Has an Items property

• Has an ItemsSource property

• Contains multiple objects

• Has an Items property

• Has an ItemsSource property

Common items controls:

• ComboBox

• ListBox

• Menu

• StatusBar

• TabControl

• ToolBar

• TreeView

Common items controls:

• ComboBox

• ListBox

• Menu

• StatusBar

• TabControl

• ToolBar

• TreeView

Can be different types

ItemsSource

Items

Page 15: Module 3 Designing and Developing a User Interface

Handling Item Selection

Attach event handlerAttach event handler

<ListBox SelectionChanged="ListBox1_SelectionChanged"> ...</ListBox>

<ListBox SelectionChanged="ListBox1_SelectionChanged"> ...</ListBox>

Define event handlerDefine event handler

public void ListBox1_SelectionChanged( object sender, SelectionChangedEventArgs e){ ...}

public void ListBox1_SelectionChanged( object sender, SelectionChangedEventArgs e){ ...}

Page 16: Module 3 Designing and Developing a User Interface

Demonstration: Creating a User Interface by Using Items Controls

In this demonstration, you will see how to use the ListBox class

Page 17: Module 3 Designing and Developing a User Interface

Lesson 4: Sharing Logical Resources in a window

• Understanding Resources

• Defining Resources

• Referencing Resources in XAML

• Referencing Resources Programmatically

Page 18: Module 3 Designing and Developing a User Interface

Understanding Resources

Resources provide a simple way to reuse commonly defined objects and valuesResources provide a simple way to reuse commonly defined objects and values

For example: brushes, styles, and control templates For example: brushes, styles, and control templates

XAML

Code

Page 19: Module 3 Designing and Developing a User Interface

Defining Resources

You can define resources at various levels:You can define resources at various levels:

• Application scope

• Window or Page scope

• Element-level scope

• Application scope

• Window or Page scope

• Element-level scope

XAMLXAML<Window.Resources> <SolidColorBrush x:Key="blueBrush" Color="Blue"/> <SolidColorBrush x:Key="whiteBrush" Color="White"/> <sys:Double x:Key="myValue">100</sys:Double></Window.Resources>

<Window.Resources> <SolidColorBrush x:Key="blueBrush" Color="Blue"/> <SolidColorBrush x:Key="whiteBrush" Color="White"/> <sys:Double x:Key="myValue">100</sys:Double></Window.Resources>

Page 20: Module 3 Designing and Developing a User Interface

Referencing Resources in XAML

To reference a resource staticallyTo reference a resource statically

PropertyName="{StaticResource ResourceKey}"PropertyName="{StaticResource ResourceKey}"

<Button Background="{StaticResource blueBrush}" Foreground="{StaticResource whiteBrush}"> Text</Button>

<Button Background="{StaticResource blueBrush}" Foreground="{StaticResource whiteBrush}"> Text</Button>

To reference a resource dynamicallyTo reference a resource dynamically

PropertyName="{DynamicResource ResourceKey}"PropertyName="{DynamicResource ResourceKey}"

<Button Background="{DynamicResource blueBrush}" Foreground="{DynamicResource whiteBrush}"> Text</Button>

<Button Background="{DynamicResource blueBrush}" Foreground="{DynamicResource whiteBrush}"> Text</Button>

Page 21: Module 3 Designing and Developing a User Interface

Referencing Resources Programmatically

FindResource methodFindResource method

SolidColorBrush brush = (SolidColorBrush) this.FindResource("whiteBrush");SolidColorBrush brush = (SolidColorBrush) this.FindResource("whiteBrush");

SetResourceReference methodSetResourceReference method

this.myButton.SetResourceReference( Button.BackgroundProperty, "whiteBrush");this.myButton.SetResourceReference( Button.BackgroundProperty, "whiteBrush");

Resources propertyResources property

SolidColorBrush brush = (SolidColorBrush) this.Resources["whiteBrush"];SolidColorBrush brush = (SolidColorBrush) this.Resources["whiteBrush"];

Or TryFindResource

Page 22: Module 3 Designing and Developing a User Interface

Lab: Building a User Interface

• Exercise 1: Choosing User Interface Controls

• Exercise 2: Laying Out the User Interface

• Exercise 3: Creating and Using Resource Dictionaries

Logon information

Estimated time: 75 minutes

Page 23: Module 3 Designing and Developing a User Interface

Lab Scenario

You have been asked to design and write the UI for the Work Orders application that was described in Module 1. You must first identify the appropriate WPF controls to use to display and manipulate the Work Orders data and then build the UI by using these controls with XAML.

Page 24: Module 3 Designing and Developing a User Interface

Lab Review

Review Questions

• Why is the Grid control a good choice for general layout purposes?

• Why is the ListView control a good choice for displaying live data?

• Why would you want to put resources in a ResourceDictionary element?

Page 25: Module 3 Designing and Developing a User Interface

Module Review and Takeaways

• Review Questions

• Best Practices