CPSC Tutorial 6

Similar documents
CPSC Tutorial 5

CPSC Tutorial 5 WPF Applications

CPSC Tutorial 9 Blend & Animations

CPSC Tutorial 4

Weather forecast ( part 2 )

CPSC 481 Tutorial 10 Expression Blend. Brennan Jones (based on tutorials by Bon Adriel Aseniero and David Ledo)

CPSC Tutorial 4 Visual Studio and C#

Styles and Control Templates

Workspace Desktop Edition Developer's Guide. Customize Views and Regions

Course 2D_SL: 2D-Computer Graphics with Silverlight Chapter C5: The Complete Code of PathAnimation. Copyright by V. Miszalok, last update:

Course 2D_WPF: 2D-Computer Graphics with C# + WPF Chapter C1a: The Intro Project Written in XAML and C#

Course 2D_SL: 2D-Computer Graphics with Silverlight Chapter C1: The Intro Project

Advanced Programming C# Lecture 3. dr inż. Małgorzata Janik

Silverlight memory board ( Part 2 )

Yes, this is still a listbox!

XAML - BUTTON. The Button class represents the most basic type of button control. The hierarchical inheritance of Button class is as follows

CS3240 Human-Computer Interaction

Week 6: First XAML Control Exercise

Windows Presentation Foundation. Jim Fawcett CSE687 Object Oriented Design Spring 2018

Getting Started with Banjos4Hire

sharpcorner.com/uploadfile/37db1d/4958/default.aspx?articleid=cb0b291c-52ae-4b80-a95c- 438d76fa1145

CS3240 Human-Computer Interaction Lab Sheet Lab Session 4 Media, Ink, & Deep Zoom

ComponentOne. HyperPanel for WPF

Lecture # 6 Engr. Ali Javed 11th March, 2014

Course 3D_WPF: 3D-Computer Graphics with C# + WPF Chapter C3: Dice

My own Silverlight textbox

Course 3D_WPF: 3D-Computer Graphics with C# + WPF Chapter C4: The complete code of Sphere

CS3240 Human-Computer Interaction Lab Sheet Lab Session 3 Designer & Developer Collaboration

Step4: Now, Drag and drop the Textbox, Button and Text block from the Toolbox.

Lesson 9: Exercise: Tip Calculator

Class Test 5. Create a simple paint program that conforms to the following requirements.

Course 3D_WPF: 3D-Computer Graphics with C# + WPF Chapter C4: Sphere

Chapter 13: Handling Events

Note: many examples in this section taken or adapted from Pro WPF 4.5 C#, Matthew MacDonald, apress, 2012, pp

Microsoft.BrainDump v by.Gabry.53q

ArcGIS Pro SDK for.net: UI Design and MVVM

NE Fundamentals of XAML and Microsoft Expression Blend

Exam Name: TS: Windows Applications Development with Microsoft.NET Framework 4

Microsoft Exam

Introduction to Data Templates and Value Converters in Silverlight

Hands-On Lab. Hello Windows Phone

Seminar Internetdienste Thema: Silverlight

Overview Describe the structure of a Windows Forms application Introduce deployment over networks

WPF Graphics and Multimedia

Exam sell. Higher Quality Better Service! Certified IT practice exam authority.

Copyright Soyatec. Licensed under the Eclipse Public License 1.0

Microsoft CSharp

CPSC 481 Tutorial 4. Intro to Visual Studio and C#

[MS10553]: Fundamentals of XAML and Microsoft Expression Blend

using System.IO; using System.Collections.Generic; using System.Xml.Linq;

Skinning Manual v1.0. Skinning Example

Microsoft TS: Silverlight 4, Development. Practice Test. Version: QQ:

ComponentOne. Extended Library for UWP

RadPDFViewer For Silverlight and WPF

Accurate study guides, High passing rate! IT TEST BOOK QUESTION & ANSWER. Ittestbook provides update free of charge in one year!

Universal Windows Platform Complete Solution

CS3240 Human-Computer Interaction Lab Sheet Lab Session 2

More Language Features and Windows Forms

This tutorial is designed for software developers who want to learn how to develop quality applications with clean structure of code.

More Language Features and Windows Forms. Part I. Some Language Features. Inheritance. Inheritance. Inheritance. Inheritance.

Hands-On Lab. Sensors -.NET. Lab version: Last updated: 12/3/2010

Using Functions in Alice

Sparkline for WPF 1. ComponentOne. Sparkline for WPF

CST242 Windows Forms with C# Page 1


Your First Windows Form

visual studio vs#d express windows desktop

Fundamentals of XAML and Microsoft Expression Blend

IAP C# Lecture 5 XAML and the Windows Presentation Foundation. Geza Kovacs

Interface Builders and Interface Description Languages

Applied WPF 4 in Context US $49.99 SOURCE CODE ONLINE

Microsoft Windows Apps Dev w/microsoft.net Framework 4. Download Full Version :

windows-10-universal #windows- 10-universal

CSC 355 PROJECT 4 NETWORKED TIC TAC TOE WITH WPF INTERFACE

ArcGIS Pro SDK for.net: Advanced User Interfaces in Add-ins. Wolfgang Kaiser

Hands-On Lab. Using Pivot and Panorama Controls

Quick Guide for the ServoWorks.NET API 2010/7/13

LAYOUT. Chapter 3 of Pro WPF : By Matthew MacDonald Assist Lect. Wadhah R. Baiee. College of IT Univ. of Babylon

Week 8: Data Binding Exercise (Bookstore)

Microsoft Corporation

Chapter 13. Additional Topics in Visual Basic The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill

Video Library: Silverlight 1.1 Case Example

This book was purchased by

WPF AND SILVERLIGHT RESOURCES

In this lecture we will briefly examine a few new controls, introduce the concept of scope, random numbers, and drawing simple graphics.

Responding to the Mouse

Windows Presentation Foundation for.net Developers

WebAqua.NET 2.0 White Paper

You can call the project anything you like I will be calling this one project slide show.

Name of Experiment: Country Database

Lab 14: PowerPoint Part 2

Week 7: NavigationView Control Exercise

Instant Silverlight 5 Animation

DOT.NET MODULE 6: SILVERLIGHT

Using Adobe Contribute 4 A guide for new website authors

Click on the empty form and apply the following options to the properties Windows.

Tutor Handbook for WebCT

Silverlight 5 Using C#

Create a memory DC for double buffering

CS 201 Advanced Object-Oriented Programming Lab 10 - Recursion Due: April 21/22, 11:30 PM

Transcription:

CPSC 481 - Tutorial 6 More WPF (based on previous tutorials by Alice Thudt, Fateme Rajabiyazdi, David Ledo, Brennan Jones, Sowmya Somanath, and Kevin Ta)

Introduction

Contact Info li26@ucalgary.ca Please always include [CPSC 481] in the subject header Expect replies in: 24 hours on weekdays 48 hours on weekends http://pages.cpsc.ucalgary.ca/~li26/481/

Reminder Horizontal Prototype Due March 6th (Monday) in class Write-up, including redesign rational (i.e. changes from the Lo-Fi prototype), screenshots, and the grading sheet (from the handout) Horizontal prototype presentation freeze Either email your slides (PDF) to me (li26@ucalgary.ca) OR submit on a USB, along with your write-up Presentations on March 7th / 9th

Horizontal Prototype

More WPF!

User Controls Controls are interactive elements in WPF Elements such as Buttons, TextBoxes, etc. are Microsoft Controls If you want to reuse specific elements in your interface, you can create User Controls User Controls can be composites of Microsoft Elements

Create User Control

Page Switcher

Create 3 buttons Pages: Method 1

Create a user control Pages (cont.)

Pages (cont.)

Fill with content Pages (cont.)

Pages (cont.) Add a StackPanel And give it a name

Pages: Initialize User Controls Create 2 additional user controls Add the following code to your MainWindow.xaml.cs before the constructor: // Initialize user controls UserControl1 _page1 = new UserControl1(); UserControl2 _page2 = new UserControl2(); UserControl3 _page3 = new UserControl3();

Pages: Result

Pages: Event Handlers private void Page1_Button_Click(object sender, RoutedEventArgs e) { stackpanel1.children.clear(); } stackpanel1.children.add(_page1); private void Page2_Button_Click(object sender, RoutedEventArgs e) { stackpanel1.children.clear(); } stackpanel1.children.add(_page2); private void Page3_Button_Click(object sender, RoutedEventArgs e) { stackpanel1.children.clear(); } stackpanel1.children.add(_page3);

Pages: Method 2 Create 3 User Controls, and name them: MainMenu, Page1, Page2 Add a Switcher.cs class to the project using System.Windows.Controls; public static MainWindow pageswitcher; public static void Switch(UserControl newpage) { pageswitcher.navigate(newpage); }

Pages (cont.) Add the following code to MainWindow.xaml.cs public MainWindow() { InitializeComponent(); Switcher.pageSwitcher = this; Switcher.Switch(new MainMenu()); } public void Navigate(UserControl nextpage) { this.content = nextpage; }

Pages (cont.) Design MainMenu, Page1, and Page2 to look like this:

Pages (cont.) For MainMenu, Page1, and Page2, add the following events: private void Button1_Click(object sender, RoutedEventArgs e) { Switcher.Switch(new MainMenu()); } private void Button2_Click(object sender, RoutedEventArgs e) { Switcher.Switch(new Page1()); } private void Button3_Click(object sender, RoutedEventArgs e) { Switcher.Switch(new Page2()); }

Pages (cont.) If you want to save the page state, then keep the User Control instance Page1 _page1 = new Page1(); private void Button_Click(object sender, RoutedEventArgs e) { Switcher.Switch(_page1); }

Style and Template

Style To customize the look-and-feel of a button Add a Resources tag

Style (cont.) Define the style

Style (cont.) OR give it a key to reuse the same style

Template You can create Control Templates These define how a control is drawn

Template (cont.) You can create Control Templates to define how a control is drawn <UserControl.Resources> <Style x:key="mybuttonstyle" TargetType="{x:Type Button} > <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Border Name="fondoboton" BorderBrush="DarkGray" BorderThickness="5" CornerRadius="10,0,10,0" Background="LightGray"> <ContentPresenter Name="contenido" Content="{Binding Path=Content, RelativeSource={RelativeSource TemplatedParent}}"></ContentPresenter> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> </UserControl.Resources>

Template (cont.) You can create Control Templates to define how a control is drawn <UserControl.Resources> <Style x:key="mybuttonstyle" TargetType="{x:Type Button} > <Setter Property= Background" Value="Orange"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Border Name="fondoboton" BorderBrush="DarkGray" BorderThickness="5" CornerRadius="10,0,10,0" Background= {TemplateBinding Background} > <ContentPresenter Name="contenido" Content="{Binding Path=Content, RelativeSource={RelativeSource TemplatedParent}}"></ContentPresenter> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> </UserControl.Resources>

Template: Override Property For a specific button, you can set the background colour to something other than the default, while keeping everything else the same as the template Without changing the template, change the button s XAML code to something like this: <Button Style= {StaticResource MyButtonStyle} Background= Red Content= Button HorizontalAlignment= Left Margin= 119,234,0,0 VerticalAlignment= Top Width= 75 >

List Box

Drag & drop List Box

List Box (cont.) Click to modify items

List Box (cont.) Select ListBoxItem Click to add

List Box (cont.) Change content

List Box: Result

List Box (cont.) Obtain the selected index listbox1.selectedindex; Add event handler private void listbox1_selectionchanged(object sender, SelectionChangedEventArgs e) { testbox1.text = (String)((ListBoxItem)listBox1.SelectedItem).Content; }

Simple Animation

Insert an ellipse Simple Animation

Simple Animation (cont.) XAML code <Ellipse Name= ellipse1" Fill= #FFE828 HorizontalAlignment= Left Height= 118 Margin= 10,151,0,0 Stroke= Black Width= 127 > Add instance declaration using System.Windows.Media.Animation; private Storyboard mystoryboard;

Simple Animation (cont.) In the User Control constructor // animate fade in and fade out DoubleAnimation animation = new DoubleAnimation(); animation.from = 1.0; animation.to = 0.0; animation.duration = new Duration(TimeSpan.FromSeconds(5)); animation.autoreverse = true; animation.repeatbehavior = RepeatBehavior.Forever; mystoryboard = new Storyboard(); mystoryboard.children.add(animation); Storyboard.SetTargetName(animation, ellipse1.name); Storyboard.SetTargetProperty(animation, new PropertyPath(Ellipse.OpacityProperty)); // Use the Loaded event to start the Storyboard. ellipse1.loaded += new RoutedEventHandler(myEllipseLoaded);

Simple Animation (cont.) Add event handler private void myellipseloaded(object sender, RoutedEventArgs e) { mystoryboard.begin(this); } How about animating the width of the ellipse?

Click & Drag

Click & Drag Create an ellipse and give a name Also give the grid a name

Click & Drag (cont.) MouseDown MouseMove MouseUp

Click & Drag (cont.) bool captured = false; private void Ellipse1_MouseDown(object sender, MouseButtonEventArgs e) { captured = true; } private void Ellipse1_MouseUp(object sender, MouseButtonEventArgs e) { captured = false; } private void Ellipse1_MouseMove(object sender, MouseEventArgs e) { if (captured) } { Thickness margin = ellipse1.margin; margin.left = e.getposition(_maingrid).x - (ellipse1.width / 2); margin.top = e.getposition(_maingrid).y - (ellipse1.height / 2); ellipse1.margin = margin; }

Trigger

Trigger Allow you to change the value of a given property one a certain condition is reached Can be done entirely in XAML 3 types: 1. Property trigger 2. Data trigger 3. Event trigger

Property Trigger Defined by the <Trigger> element Watches the change of a specific property on the owner control Whenever that property has a specific value, it changes other properties

Property Trigger Example <TextBlock Text="Hello, styled world!" FontSize="28" HorizontalAlignment="Center" VerticalAlignment="Center"> <TextBlock.Style> <Style TargetType="TextBlock"> <Setter Property="Foreground" Value="Blue"></Setter> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Foreground" Value="Red" /> <Setter Property="TextDecorations" Value="Underline" /> </Trigger> </Style.Triggers> </Style> </TextBlock.Style> </TextBlock> Result: underlines and colours the text red on mouse over

Data Trigger Defined by the <DataTrigger> element Watches a specific property that can be anywhere (not specifically on the owner control) Whenever that property has a specific value, it changes other properties

Data Trigger Example <CheckBox Name="cbSample" Content= Hello, world? /> <TextBlock Margin= 0,20,0,0 FontSize="48" HorizontalAlignment="Center"> <TextBlock.Style> <Style TargetType="TextBlock"> <Setter Property="Text" Value= No"></Setter> <Setter Property="Foreground" Value="Red"></Setter> <Style.Triggers> <DataTrigger Binding= {Binding ElementName=cbSample, Path=IsChecked} Value="True"> <Setter Property="Foreground" Value="Green" /> <Setter Property="Text" Value= Yes!" /> </DataTrigger> </Style.Triggers> </Style> </TextBlock.Style> </TextBlock> Result: Changes the text to Yes! and colour to green when the checkbox is checked

Event Trigger Defined by the <EventTrigger> element Triggers in response to an event being called Triggers exactly once that event is called

Event Trigger Example <TextBlock Name="lblStyled" Text="Hello, styled world!" FontSize="18" HorizontalAlignment="Center" VerticalAlignment="Center"> <TextBlock.Style> <Style TargetType="TextBlock"> <Style.Triggers> <EventTrigger RoutedEvent= MouseEnter > <EventTrigger.Actions> <BeginStoryboard> <Storyboard> <DoubleAnimation Duration= 0:0:0.300 Storyboard.Targetproperty= FontSize" To="28"> </Storyboard> </BeginStoryboard> </EventTrigger.Actions> </EventTrigger> <EventTrigger RoutedEvent= MouseLeave > <EventTrigger.Actions> <BeginStoryboard> <Storyboard> <DoubleAnimation Duration= 0:0:0.800 Storyboard.Targetproperty= FontSize" To="18"> </Storyboard> </BeginStoryboard> </EventTrigger.Actions> </EventTrigger> </Style.Triggers> </Style> </TextBlock.Style> </TextBlock>

Event Trigger Example (cont.) Result: animation enlarges the text on mouse over, and thinks it back to its original size on mouse leave

Image as Button

Image as Button Create a button that displays as an image <Grid> <Button Background="Transparent" HorizontalAlignment="Left" Margin="117,84,0,0" VerticalAlignment="Top" Width="361" Height="231"> <Image Name="img1" Stretch="Fill" Source="Testimg.png" Margin= 10" /> </Button> </Grid>

Timer

Timer We are going to make use of a DispatchTimer Make sure you are using the Threading namespace Add a <TextBlock> on your code, and we will change the value every time the timer ticks

Timer (cont.) XAML <Grid Name="grid" > <TextBlock Name="TimerText" FontSize="48" VerticalAlignment= Center" HorizontalAlignment= Center" /> </Grid> C# using System.Windows.Threading; public void DispatcherTimerSample() { InitializeComponent(); DispatcherTimer timer = new DispatcherTimer(); timer.interval = TimeSpan.FromSeconds(1); timer.tick += timer_tick; timer.start(); } void timer_tick(object sender, EventArgs e) { timertext.content = DateTime.Now.ToLongTimeString(); }

Exercise Try a few of the things we talked today

Next Week Horizontal Prototype Presentations 12 mins + 3 mins feedback All group members must attend

Thank You