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

Similar documents
CPSC Tutorial 5

CPSC Tutorial 5 WPF Applications

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

Copyright Soyatec. Licensed under the Eclipse Public License 1.0

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

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

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

WPF Viewer for Reporting Services 2008/2012 Getting Started

Interface Builders and Interface Description Languages

Applied WPF 4 in Context US $49.99 SOURCE CODE ONLINE

RadPDFViewer For Silverlight and WPF

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

Wpf Button Click Event Firing Multiple Times

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

ComponentOne. PdfViewer for WPF and Silverlight

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

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

Introduction to Data Templates and Value Converters in Silverlight

Mobile Programming Lecture 1. Getting Started

CPSC Tutorial 6

WPF and MVVM Study Guides

Lesson 9: Exercise: Tip Calculator

Cross Platform Development Windows 8 Windows Phone 8

Chromatic Remote Control Product Guide Executive Way, Suite A Frederick, MD 21704

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

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

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

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

Chapter 1. Introducing WPF. Objectives: Understand the motivation behind WPF. Examine the various flavors of WPF applications

Quick Multitouch Apps using kivy and Python

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

(Refer Slide Time: 1:12)

Name of Experiment: Student Database

Getting Started with Banjos4Hire

Windows Presentation Foundation

CPSC Tutorial 9 Blend & Animations

Xamarin.Forms. Pages Building an Interface

CHANNEL9 S WINDOWS PHONE 8.1 DEVELOPMENT FOR ABSOLUTE BEGINNERS

Portable Class Libraries ---

Silverlight memory board ( Part 2 )

Fundamentals of XAML and Microsoft Expression Blend

Microsoft Silverlight 4 Business Application Development

Week 6: First XAML Control Exercise

10/9/2012. Sample C# program:

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

[MS10553]: Fundamentals of XAML and Microsoft Expression Blend

Beginning Silverlight 5 in C #

Xamarin.Forms. Pages Building an Interface

Week 7: NavigationView Control Exercise

The following are required to duplicate the process outlined in this document.

Weather forecast ( part 2 )

WPF Debugging and Performance Succinctly

ComponentOne. GanttView for WPF

Windows Presentation Foundation Programming Using C#

CS3240 Human-Computer Interaction Lab Sheet Lab Session 2

Your First Windows Form

NE Fundamentals of XAML and Microsoft Expression Blend

COMP-202: Foundations of Programming. Lecture 4: Methods Jackie Cheung, Winter 2016

Microsoft CSharp

Preliminary 1: Download and install the Certificate Authority (CA) certificate

CMSC434. Introduction to Human-Computer Interaction. Week 10 Lecture 16 Mar 29, 2016 Engineering Interfaces II. Jon

S A M P L E C H A P T E R

Tutorial 6 Enhancing the Inventory Application Introducing Variables, Memory Concepts and Arithmetic

EXCEL + POWERPOINT. Analyzing, Visualizing, and Presenting Data-Rich Insights to Any Audience KNACK TRAINING

ComponentOne. HyperPanel for WPF

Creating and Running Your First C# Program

windows-10-universal #windows- 10-universal

EEE-425 Programming Languages (2013) 1

Featured Products Extension for Magento 2

MATFOR In Visual C# ANCAD INCORPORATED. TEL: +886(2) FAX: +886(2)

Event-based Programming

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

Launchers and Choosers Hands-on Lab. Hands-On Lab. Launchers and Choosers. Lab version: Last updated: 12/8/2010. Page 1

Agent Team Lite Window Phone 7 Apps

Mobile Development Workshop DAY 2: INTRODUCTION TO JAVA

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

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

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

Workspace Desktop Edition Developer's Guide. Best Practices for Views

Windows Presentation Foundation (WPF)

Real World. static methods and Console & JavaFX App Intros. Lecture 16. Go ahead and PULL Lecture Materials & Sign-in on PollEv

Hands-On Lab. Building Applications in Silverlight 4 Module 6: Printing the Schedule. Printing the Schedule

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

XAML. Chapter 2 of Pro WPF : By Matthew MacDonald Assist Lect. Wadhah R. Baiee. College of IT Univ. of Babylon Understanding XAML

AP Computer Science Unit 1. Writing Programs Using BlueJ

Week 8: Data Binding Exercise (Bookstore)

Developing Desktop Apps for Ultrabook Devices in Windows* 8: Adapting Existing Apps By Paul Ferrill

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

AP Computer Science Unit 1. Programs

Microsoft Corporation

COMP-202: Foundations of Programming. Lecture 26: Review; Wrap-Up Jackie Cheung, Winter 2016

Widgets. Widgets Widget Toolkits. 2.3 Widgets 1

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

Introduction to Graphical Interface Programming in Java. Introduction to AWT and Swing

BASICS OF GRAPHICAL APPS

New Products Extension for Magento 2

KillTest. 半年免费更新服务

1A Windows Presentation Foundation Explained. Rob Layzell CA Technologies

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

Mobile Computing Professor Pushpedra Singh Indraprasth Institute of Information Technology Delhi Andriod Development Lecture 09

Transcription:

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

What is Windows Presentation Foundation (WPF)? A toolkit for building graphical user interfaces (GUI) for an application Ships as part of the.net platform for Windows, Windows Phone 7, and Silverlight

Elements of a GUI application The frontend / interface: where are the controls (buttons, labels, sliders, text boxes) placed, how does the application look like to your user? The backend code: when the user clicks a button, what code gets executed?

Coding a GUI Each control (button, label, slider, text box) is represented by an object Each object representing a control has some properties: (ex: the text on a button) Writing an GUI in C#: create objects representing your Window and each of the controls, set all their properties, then show the interface

Displaying a Window with a Button using System; static class MyMainClass [STAThread] static void Main(string[] args) Window window = new Window(); window.title = "Hello World"; button.content = "Click Me"; button.fontsize = 32.0; window.content = button; window.show(); Application app = new Application(); app.run();

Displaying a Window with a Button using System; static class MyMainClass [STAThread] static void Main(string[] args) Window window = new Window(); Defines a Window window.title = "Hello World"; button.content = "Click Me"; button.fontsize = 32.0; window.content = button; window.show(); Application app = new Application(); app.run();

Displaying a Window with a Button using System; static class MyMainClass [STAThread] static void Main(string[] args) Window window = new Window(); window.title = "Hello World"; Sets the Window s title button.content = "Click Me"; button.fontsize = 32.0; window.content = button; window.show(); Application app = new Application(); app.run();

Displaying a Window with a Button using System; static class MyMainClass [STAThread] static void Main(string[] args) Window window = new Window(); window.title = "Hello World"; Defines a Button control button.content = "Click Me"; button.fontsize = 32.0; window.content = button; window.show(); Application app = new Application(); app.run();

Displaying a Window with a Button using System; static class MyMainClass [STAThread] static void Main(string[] args) Window window = new Window(); window.title = "Hello World"; button.content = "Click Me"; Sets the text in the button button.fontsize = 32.0; window.content = button; window.show(); Application app = new Application(); app.run();

Displaying a Window with a Button using System; static class MyMainClass [STAThread] static void Main(string[] args) Window window = new Window(); window.title = "Hello World"; button.content = "Click Me"; button.fontsize = 32.0; Sets the button text font size window.content = button; window.show(); Application app = new Application(); app.run();

Displaying a Window with a Button using System; static class MyMainClass [STAThread] static void Main(string[] args) Window window = new Window(); window.title = "Hello World"; button.content = "Click Me"; button.fontsize = 32.0; window.content = button; Adds the button to the window window.show(); Application app = new Application(); app.run();

Displaying a Window with a Button using System; static class MyMainClass [STAThread] static void Main(string[] args) Window window = new Window(); window.title = "Hello World"; button.content = "Click Me"; button.fontsize = 32.0; window.content = button; window.show(); Shows the window Application app = new Application(); app.run();

Displaying a Window with a Button using System; static class MyMainClass [STAThread] static void Main(string[] args) Window window = new Window(); window.title = "Hello World"; button.content = "Click Me"; button.fontsize = 32.0; window.content = button; window.show(); Application app = new Application(); app.run(); Needed to start a WPF application

Adding Interactivity to an application Whenever the user does something to the GUI (like, clicking a button), an event will be triggered You can have a method be called whenever an event occurs (ie, a button is clicked), by subscribing to the event

Whenever the user does something to the GUI (like, clicking a button), an event will be triggered You can have a method be called whenever an event occurs, by subscribing to the event static void PrintHelloWorld(object sender, RoutedEventArgs e) Console.WriteLine("Hello World"); static void Main(string[] args) button.click += PrintHelloWorld;

Whenever the user does something to the GUI (like, clicking a button), an event will be triggered You can have a method be called whenever an event occurs, by subscribing to the event static void PrintHelloWorld(object sender, RoutedEventArgs e) Console.WriteLine("Hello World"); static void Main(string[] args) button.click += PrintHelloWorld; An event

Whenever the user does something to the GUI (like, clicking a button), an event will be triggered You can have a method be called whenever an event occurs, by subscribing to the event static void PrintHelloWorld(object sender, RoutedEventArgs e) Console.WriteLine("Hello World"); static void Main(string[] args) button.click += PrintHelloWorld; Subscribing to an event

Whenever the user does something to the GUI (like, clicking a button), an event will be triggered You can have a method be called whenever an event occurs, by subscribing to the event static void PrintHelloWorld(object sender, RoutedEventArgs e) Console.WriteLine("Hello World"); static void Main(string[] args) button.click += PrintHelloWorld; Which method to call whenever event occurs

Whenever the user does something to the GUI (like, clicking a button), an event will be triggered You can have a method be called whenever an event occurs, by subscribing to the event static void PrintHelloWorld(object sender, RoutedEventArgs e) Console.WriteLine("Hello World"); Method s signature depends on the event static void Main(string[] args) button.click += PrintHelloWorld;

Whenever the user does something to the GUI (like, clicking a button), an event will be triggered You can have a method be called whenever an event occurs, by subscribing to the event static void PrintSomethingElse(object sender, RoutedEventArgs e) Console.WriteLine("Something Else"); static void PrintHelloWorld(object sender, RoutedEventArgs e) Console.WriteLine("Hello World"); static void Main(string[] args) button.click += PrintHelloWorld;

Whenever the user does something to the GUI (like, clicking a button), an event will be triggered You can have a method be called whenever an event occurs, by subscribing to the event static void PrintSomethingElse(object sender, RoutedEventArgs e) Console.WriteLine("Something Else"); static void PrintHelloWorld(object sender, RoutedEventArgs e) Console.WriteLine("Hello World"); static void Main(string[] args) button.click += PrintHelloWorld; button.click += PrintSomethingElse; Can subscribe multiple methods to an event; all will be called whenever button is pressed

using System; static class MyMainClass static void PrintHelloWorld(object sender, RoutedEventArgs e) Console.WriteLine("Hello World"); static void PrintSomethingElse(object sender, RoutedEventArgs e) Console.WriteLine("Something Else"); [STAThread] static void Main(string[] args) Window window = new Window(); window.title = "Hello World"; button.content = "Click Me"; button.fontsize = 32.0; button.click += PrintHelloWorld; button.click += PrintSomethingElse; window.content = button; window.show(); Application app = new Application(); app.run();

using System; static class MyMainClass static void PrintHelloWorld(object sender, RoutedEventArgs e) Console.WriteLine("Hello World"); static void PrintSomethingElse(object sender, RoutedEventArgs e) Console.WriteLine("Something Else"); [STAThread] static void Main(string[] args) Window window = new Window(); window.title = "Hello World"; button.content = "Click Me"; button.fontsize = 32.0; button.click += PrintHelloWorld; button.click += PrintSomethingElse; window.content = button; window.show(); Application app = new Application(); app.run(); Define a window, set its title

using System; static class MyMainClass static void PrintHelloWorld(object sender, RoutedEventArgs e) Console.WriteLine("Hello World"); static void PrintSomethingElse(object sender, RoutedEventArgs e) Console.WriteLine("Something Else"); [STAThread] static void Main(string[] args) Window window = new Window(); window.title = "Hello World"; button.content = "Click Me"; button.fontsize = 32.0; button.click += PrintHelloWorld; button.click += PrintSomethingElse; window.content = button; window.show(); Application app = new Application(); app.run(); Define a new button, set the button text and font size

using System; static class MyMainClass static void PrintHelloWorld(object sender, RoutedEventArgs e) Console.WriteLine("Hello World"); static void PrintSomethingElse(object sender, RoutedEventArgs e) Console.WriteLine("Something Else"); [STAThread] static void Main(string[] args) Window window = new Window(); window.title = "Hello World"; button.content = "Click Me"; button.fontsize = 32.0; button.click += PrintHelloWorld; button.click += PrintSomethingElse; window.content = button; window.show(); Application app = new Application(); app.run(); Subscribe methods to the button s click event (both called when clicked)

using System; static class MyMainClass static void PrintHelloWorld(object sender, RoutedEventArgs e) Console.WriteLine("Hello World"); static void PrintSomethingElse(object sender, RoutedEventArgs e) Console.WriteLine("Something Else"); [STAThread] static void Main(string[] args) Window window = new Window(); window.title = "Hello World"; button.content = "Click Me"; button.fontsize = 32.0; button.click += PrintHelloWorld; button.click += PrintSomethingElse; window.content = button; window.show(); Application app = new Application(); app.run(); Add button to the window

using System; static class MyMainClass static void PrintHelloWorld(object sender, RoutedEventArgs e) Console.WriteLine("Hello World"); static void PrintSomethingElse(object sender, RoutedEventArgs e) Console.WriteLine("Something Else"); [STAThread] static void Main(string[] args) Window window = new Window(); window.title = "Hello World"; button.content = "Click Me"; button.fontsize = 32.0; button.click += PrintHelloWorld; button.click += PrintSomethingElse; window.content = button; window.show(); Application app = new Application(); app.run(); Show the window

using System; static class MyMainClass static void PrintHelloWorld(object sender, RoutedEventArgs e) Console.WriteLine("Hello World"); static void PrintSomethingElse(object sender, RoutedEventArgs e) Console.WriteLine("Something Else"); [STAThread] static void Main(string[] args) Window window = new Window(); window.title = "Hello World"; button.content = "Click Me"; button.fontsize = 32.0; button.click += PrintHelloWorld; button.click += PrintSomethingElse; window.content = button; window.show(); Application app = new Application(); app.run(); Start the application

using System; static class MyMainClass static void PrintHelloWorld(object sender, RoutedEventArgs e) Console.WriteLine("Hello World"); static void PrintSomethingElse(object sender, RoutedEventArgs e) Console.WriteLine("Something Else"); [STAThread] static void Main(string[] args) Window window = new Window(); window.title = "Hello World"; button.content = "Click Me"; button.fontsize = 32.0; button.click += PrintHelloWorld; button.click += PrintSomethingElse; window.content = button; window.show(); Application app = new Application(); app.run(); Start the application

Layouts So far, we ve had only 1 button. What if we want multiple buttons (or textbox, label, or other controls) on screen? In WPF, we usually use a layout to organize multiple widgets on screen StackPanel: stacks items horizontally or vertically Grid: organizes them into columns and rows

using System; static class MyMainClass [STAThread] static void Main(string[] args) Window window = new Window(); window.title = "Hello World"; window.show(); Button button1 = new Button(); button1.fontsize = 36.0; button1.content = "Button 1"; Button button2 = new Button(); button2.fontsize = 36.0; button2.content = "Button 2"; StackPanel panel = new StackPanel(); panel.children.add(button2); panel.children.add(button1); window.content = panel; Application app = new Application(); app.run(); StackPanel is a layout for organizing our 2 buttons

using System; static class MyMainClass [STAThread] static void Main(string[] args) Window window = new Window(); window.title = "Hello World"; window.show(); Button button1 = new Button(); button1.fontsize = 36.0; button1.content = "Button 1"; Button button2 = new Button(); button2.fontsize = 36.0; button2.content = "Button 2"; StackPanel panel = new StackPanel(); panel.orientation = Orientation.Horizontal; panel.children.add(button2); panel.children.add(button1); window.content = panel; Application app = new Application(); app.run(); Can change orientation

Separation of frontend and backend The frontend / interface: where are the buttons placed, how does the application look like to your user? The backend code: when the user clicks a button, what code gets executed? GUI applications should try to keep these are separate as possible Makes it easy to replace interface while keeping the backend code working correctly In WPF, this is accomplished via XAML

XAML A specialized (XML-based) language for defining an interface in WPF Can describe an interface more concisely than simply using C# Ex: defining a button in WPF vs in C#: <Button Content="Click Me" FontSize="32" /> button.content = "Click Me"; button.fontsize = 32.0;

Defining a stack layout in XAML vs C# <StackPanel> <Button Content="Button 2" FontSize="32" /> <Button Content="Button 1" FontSize="32" /> </StackPanel> Button button1 = new Button(); button1.fontsize = 36.0; button1.content = "Button 1"; Button button2 = new Button(); button2.fontsize = 36.0; button2.content = "Button 2"; StackPanel panel = new StackPanel(); panel.children.add(button2); panel.children.add(button1);

Defining a stack layout in XAML vs C# <StackPanel Orientation="Horizontal"> <Button Content="Button 2" FontSize="32" /> <Button Content="Button 1" FontSize="32" /> </StackPanel> Button button1 = new Button(); button1.fontsize = 36.0; button1.content = "Button 1"; Button button2 = new Button(); button2.fontsize = 36.0; button2.content = "Button 2"; StackPanel panel = new StackPanel(); panel.orientation = Orientation.Horizontal; panel.children.add(button2); panel.children.add(button1);

<Window x:class="mywindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow"> <StackPanel Orientation="Horizontal"> <Button Content="Button 2" FontSize="32" /> <Button Content="Button 1" FontSize="32" /> </StackPanel> </Window> MyWindow.xaml the interface

<Window x:class="mywindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow"> <StackPanel Orientation="Horizontal"> <Button Content="Button 2" FontSize="32" /> <Button Content="Button 1" FontSize="32" /> </StackPanel> </Window> MyWindow.xaml the interface public partial class MyWindow : Window public MyWindow() InitializeComponent(); MyWindow.xaml.cs the backend logic for MyWindow (none at the moment)

<Window x:class="mywindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow"> <StackPanel Orientation="Horizontal"> <Button Content="Button 2" FontSize="32" /> <Button Content="Button 1" FontSize="32" /> </StackPanel> </Window> MyWindow.xaml the interface public partial class MyWindow : Window public MyWindow() InitializeComponent(); MyWindow.xaml.cs the backend logic for MyWindow (none at the moment) using System; static class MyMainClass [STAThread] static void Main(string[] args) MyWindow window = new MyWindow(); window.show(); new Application().Run(); MyMainClass.cs creates a MyWindow, shows it, and starts the WPF application

<Window x:class="mywindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow"> <StackPanel Orientation="Horizontal"> <Button Content="Button 2" FontSize="32" /> <Button Content="Button 1" FontSize="32" /> </StackPanel> </Window> public partial class MyWindow : Window public MyWindow() InitializeComponent(); public void DoSomething1(object sender, RoutedEventArgs e) Console.WriteLine("button 1 clicked"); Suppose we want some method in MyWindow.xaml.cs to be called whenever button 1 gets clicked (an event)

<Window x:class="mywindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow"> <StackPanel Orientation="Horizontal"> <Button Content="Button 2" FontSize="32" /> <Button Content="Button 1" FontSize="32" Click="DoSomething1" /> </StackPanel> </Window> public partial class MyWindow : Window public MyWindow() InitializeComponent(); public void DoSomething1(object sender, RoutedEventArgs e) Console.WriteLine("button 1 clicked"); Subscribes DoSomething1 method to button1 s Click event Executed whenever button is clicked Suppose we want some method in MyWindow.xaml.cs to be called whenever button 1 gets clicked Add Click=DoSomething1 in XAML to subscribe the method to the event

<Window x:class="mywindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow"> <StackPanel> <Label Content="Enter Text" /> TextBox <TextBox /> </StackPanel> </Window> using System; public partial class MyWindow : Window public MyWindow() InitializeComponent(); Label TextBox control user can enter text in it Label control displays some message

<Window x:class="mywindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow"> <StackPanel> <Label Content="Enter Text" /> <TextBox TextChanged="RunWhenTextChanges /> </StackPanel> </Window> Subscribe RunWhenTextChanges to TextChanged event using System; public partial class MyWindow : Window public MyWindow() InitializeComponent(); public void RunWhenTextChanges(object sender, TextChangedEventArgs e) Console.WriteLine("text changed"); Executed whenever text changes TextBox s TextChanged event is triggered whenever user enters text into the textbox

<Window x:class="mywindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow"> <StackPanel> <Label Content="Enter Text" /> <TextBox TextChanged="RunWhenTextChanges" Name="textBoxA" /> </StackPanel> </Window> Can refer to this TextBox as textboxa in code using System; public partial class MyWindow : Window public MyWindow() InitializeComponent(); public void RunWhenTextChanges(object sender, TextChangedEventArgs e) Console.WriteLine(textBoxA.Text); textboxa.text: text entered in the TextBox Suppose we want to retrieve text from the TextBox we ll need the name of the instance. Can specify this in XAML using the Name property

<Window x:class="mywindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow"> <StackPanel> <Label Content="Enter Text" /> <TextBox TextChanged="RunWhenTextChanges" Name="textBoxA" /> </StackPanel> </Window> Can refer to this TextBox as textboxa in code using System; public partial class MyWindow : Window public MyWindow() InitializeComponent(); public void RunWhenTextChanges(object sender, TextChangedEventArgs e) Console.WriteLine(textBoxA.Text); textboxa.text: text entered in the TextBox Suppose we want to retrieve text from the TextBox we ll need the name of the instance. Can specify this in XAML using the Name property

<Window x:class="mywindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow"> <StackPanel> <Label Content="Slide the slider" /> <Slider Minimum="0" Maximum="100" Name="sliderA" ValueChanged="sliderA_ValueChanged" /> </StackPanel> </Window> using System; public partial class MyWindow : Window public MyWindow() InitializeComponent(); public void slidera_valuechanged(object sender, RoutedPropertyChangedEventArgs<double> Console.WriteLine(sliderA.Value); slidera.value: value in the slider Slider: can be slid by user, between a Minimum and Maximum value. ValueChanged event occurs when user slides the slider

<Window x:class="mywindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow"> <StackPanel> <Slider Minimum="0" Maximum="100" Name="sliderA" ValueChanged="sliderA_ValueChanged" /> <TextBox Name="someTextBox" /> </StackPanel> </Window> using System; public partial class MyWindow : Window public MyWindow() InitializeComponent(); public void slidera_valuechanged(object sender, RoutedPropertyChangedEventArgs<double> sometextbox.text = slidera.value.tostring(); Sets text in sometextbox whenever slider value changes