V 1.0 Programming III. XAML Data Binding I.. V 1.0ÓE-NIK, 2014 XAML namespaces Namespaces define the allowed tags and attributes The one without the prefix.

Slides:



Advertisements
Similar presentations
Module 1: Creating an Application by Using Windows Presentation Foundation Overview of WPF Creating a Simple WPF Application Handling Events and Commands.
Advertisements

Expression Blend 4 – deep dive
Data Binding in GUI Application Development Patrick O’Brien.
Dinko Jakovljević Microsoft Student Partner | BambooLab
Visual Basic 2010 How to Program. © by Pearson Education, Inc. All Rights Reserved.2.
An Introduction To Silverlight Gergely Orosz
All sections to appear here. pptPlex Section Divider Introduction The slides after this divider will be grouped into a section and given the label you.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
1 Applets Chapter 1 To understand:  why applets are used to extend the capabilities of Web pages  how an applet is executed and know about the restrictions.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
Tutorial: Introduction to ASP.NET Internet Technologies and Web Application 4 th February 2010.
Database-Driven Web Sites, Second Edition1 Chapter 8 Processing ASP.NET Web Forms and Working With Server Controls.
.NET Database Technologies: Introduction to WPF and Entity Framework DataBinding.
V 1.0 Programming III. XML XAML Data Binding I.. V 1.0ÓE-NIK, 2014 XML (w3schools.com) A hierarchical way of defining data XML declaration + elements.
V 1.0 Programming III. Creation of additional windows Routed events.
1 COS240 O-O Languages AUBG, COS dept Lecture 33 Building Apps Technologies C# (WPF part 1)
Next Generation for Desktop Applications Doncho Minkov Telerik School Academy schoolacademy.telerik.com Technical Trainer
1 Intro XAML Attribute syntax & property syntax Panels Reusable resources Controls Data binding Steen Jensen, spring 2014.
V 1.1 Programming III. GUI APIs WPF Hello World Important UI elements UI elements / content models UI elements / inheritance.
Windows Presentation Foundation. Agenda Introduction Developing Applications WPF and WF interoperability Custom Controls Styles and Templates Data Binding.
V 1.1 Programming III. Important events, properties, methods of UI elements XAML basics.
 This presentation introduces the following: › 3 types of CSS › CSS syntax › CSS comments › CSS and color › The box model.
V 1.0 Programming III. Comparison of GUI APIs WPF Hello World UI elements Content models Inheritance chains.
Windows Presentation Foundation Adam Calderon Principal Engineer Interknowlogy LLC
Adam Calderon – C# MVP Application Development Practice Lead Interknowlogy.
GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.
V 1.1 Programming III. Creating additional windows Event handling: preview/routed events.
Basic WPF Controls Doncho Minkov Telerik School Academy schoolacademy.telerik.com Technical Trainer
V 1.0 Programming III. Automatic notifications (…Changed, INofityPropertyChanged, ObservableCollection ) Data formatters Data conversions Resources.
LAYOUT CONTROLS. XAML Overview XAML : eXtensible Application Markup Language pronounced (ZAMEL) is a markup language used to design user interfaces XML-based.
Object Oriented Software Development 9. Creating Graphical User Interfaces.
VB and C# Programming Basics. Overview Basic operations String processing Date processing Control structures Functions and subroutines.
Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Presentation Foundation Ruwan Wijesinghe.
John Daintree Data Binding Reloaded. Data Binding (2013) Data Binding Reloaded (2014) Data Binding Revolutions (2015) The Databinding Trilogy.
4 Chapter Four Introduction to HTML. 4 Chapter Objectives Learn basic HTML commands Discover how to display graphic image objects in Web pages Create.
Windows Presentation Foundation (WPF). Introduction Separates appearance of user interface from behavior Appearance usually specified by XAML Behavior.
More exercises with C# Fateme Rajabi #W6. Add an image to your project Right click on your project name in solution explorer Add -> Existing item -> browse.
V 1.0 Programming III. Converters Resources. V 1.0ÓE-NIK, 2014 Converters Data conversion: –Can be automatic! E.g.: SolidColorBrush  string, int  string.
HTML(Hyper Text Markup Language) ByNaveen. Introduction HTML or Hyper Text Markup Language is the standard markup language Its used to create the web.
ControlTemplate and DataTemplate Doncho Minkov Telerik School Academy Technical Trainer
1 Catching up on Rich Clients (round 1) Mike Ormond, Mike Taulty Developer & Platform Group Microsoft Ltd
Module 4 Taking Control of the User Interface. Module Overview Sharing Logical Resources in an Application Creating Consistent User Interfaces by Using.
HTML Basics. HTML Coding HTML Hypertext markup language The code used to create web pages.
Creating a Java Application and Applet
V 1.0 Programming III. Automatic notifications with data binding (…Changed, INofityPropertyChanged, ObservableCollection, DataTemplate) Data formatters.
CPSC 481 – Week #7 Sowmya Somanath
Module 1 Introducing C# and the.NET Framework. Module Overview Introduction to the.NET Framework 4 Creating Projects Within Visual Studio 2010 Writing.
1 HTML: HyperText Markup Language Representation and Management of Data on the Internet.
V 1.0 Programming III. Visual/FrameworkElement descendants.
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Chapter 7 Representing Web Data:
Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Presentation Foundation Ruwan Wijesinghe.
Windows 8 apps and the MVVM pattern SILVERLIGHTSHOW.NET WEBINARS SERIES GILL CLEEREN, October 16 th
HTML Tutorial. What is HTML HTML is a markup language for describing web documents (web pages) HTML documents are described by HTML tags Each HTML tag.
Soyatec Contents Needs Architecture XAML fundamentals Data Binding Advanced features Style Q&A.
WPF (Avalon), Windows App GUI Windows App GUI Web App GUI Web App GUI HTML HTML XML XML WPF WPF LINQ LINQ.
Interstage BPM v11.2 1Copyright © 2010 FUJITSU LIMITED FORMS.
Part of the Microsoft.NET Framework 3.0 Tomer Shamam.NET Technologies Expert Sela Group
Data Binding, Binding Properties Doncho Minkov Telerik School Academy Technical Trainer
Adam Schultz MVVM and WPF. MVVM Model, View, ViewModel A software architecture designed to separate out User Interface design, Business Logic, and Data.
Jim Fawcett, Brown Bag Seminar Series Fall 2007
HTML Basics.
Unit 4 Representing Web Data: XML
Ben Riga 02 | Basics of View Models Ben Riga
Jim Fawcett, Brown Bag Seminar Series Fall 2007
XAML User Interface Creation in C#
This Week: Tkinter for GUI Interfaces Some examples
Chapter 7 Representing Web Data: XML
Resources & Controls AKEEL AHMED.
An introduction to MVVM using WPF NISCHAL S
Data/Code driven UI using WPF part 2
Presentation transcript:

V 1.0 Programming III. XAML Data Binding I.

V 1.0ÓE-NIK, 2014 XAML namespaces Namespaces define the allowed tags and attributes The one without the prefix (xmlns) allow us to use the tags defined in the referenced namespace without any prefix (e.g. Window, Grid...) The one with prefix (xmlns:x) allow us to use the tags defined in the referenced namespace with the specified prefix (x:Name, x:FieldModifier) 2 <Window x:Class="WpfApplication4.MainWindow” xmlns=" xmlns:x=" Title="MainWindow" Height="350" Width="525">

V 1.0ÓE-NIK, 2014 XAML namespaces This imports the most basic WPF-centered.NET namespaces into the XAML: –System.Windows –System.Windows.Controls –System.Windows.Data –System.Windows.Media –System.Windows.Navigation... etc. No need for prefixes in tags/attributes 3 <Window x:Class="WpfApplication4.MainWindow” xmlns=" xmlns:x=" <Window x:Class="WpfApplication4.MainWindow” xmlns=" xmlns:x=" Title="MainWindow" Height="350" Width="525">

V 1.0ÓE-NIK, 2014 XAML namespaces This imports the XAML-specific keywords and some of the types in System.Windows.Markup : –Class, Null, Static, Array, ClassModifier, FieldModifier, DynamicResource, StaticResource, Key, Name, Code, … Only a few of them will be used, the prefix is usually x Name vs x:Name; “See also AutomationProperties.Name VS x:Name, AutomationProperties.Name is used by accessibility tools and some testing tools.” 4 <Window x:Class="WpfApplication4.MainWindow” xmlns=" xmlns:x=" <Window x:Class="WpfApplication4.MainWindow” x:ClassModifier="internal" xmlns=" xmlns:x=" Title="MainWindow" Height="350" Width="525"> <Button Content="START!" Name="buttonStart” x:FieldModifier="public"/>

V 1.0ÓE-NIK, 2014 XAML namespaces We can import any arbitrary.NET namespace –clr-namespace:name_of_namespace NO SPACES ALLOWED!!! –assembly: Typically DLL/EXE file that contains the class (without the extension) – refer to the manual 5 <Window x:Class="WpfApplication4.MainWindow"... xmlns:System="clr-namespace:System;assembly=mscorlib”> Hello World

V 1.0ÓE-NIK, 2014 XAML markup extensions Using this syntax, we can specify complex content into one single attribute: using string, we create a simple new object Usage cases: –Null value (Content="{x:Null}“) –Associate content with an already existing instance/static class/property –Create instance with a non-default constructor –Data binding! “Take the value from another class” –etc. Format: Every markup extension has its own syntax/attributes 6 <SomeTag SomeAttribute="{…}"

V 1.0ÓE-NIK, 2014 Data Binding I. Connect a property of a UI element with a property of another object instance –When the instance property changes, the GUI should automatically refresh –When the GUI changes, the instance property should change as well –Usually this “object instance” is a simple class instance, but it can also be another UI element E.g.: Display a person in a window –txtName.Text  personInstance.Name –chkHoliday.IsChecked  personInstance.OnHoliday –chkSick.IsChecked  personInstance.IsSick 7

V 1.0ÓE-NIK, 2014 Data Binding I. E.g.: –Volume slider: the Label near it should display the current volume label.Content  slider.Value E.g.: –The RadioButtons should only be enabled if the checkbox is checked radioButtonXXX.IsEnabled  checkBox.IsChecked Or, if the radio buttons have the same parent control: stackPanel.IsEnabled  checkBox.IsChecked 8

V 1.0ÓE-NIK, 2014 DataBinding I. E.g.: –The ListBox should display an array listBox.ItemsSource  array E.g.: –This ListBox should have a border color that matches the selected element listBox.BorderBrush  listBox.SelectedItem Data types??? E.g.: –The selected item’s data should be shown labelXXXX.Content  comboBox.SelectedItem XXXX 9

V 1.0ÓE-NIK, 2014 Data Binding I. Source, Target, Mode, Refresh strategy Source and Target –The source is the data itself or a (public) property –The target is usually the GUI element (the property that will display/use the data) The target property must be a Dependency Property (most of the UI elements’ properties are such properties) The target control must be a DependencyObject descendant 10 E.g.: Display a person in a window –txtName.Text  personInstance.Name –chkHoliday.IsChecked  personInstance.OnHoliday –chkSick.IsChecked  personInstance.IsSick

V 1.0ÓE-NIK, 2014 Data Binding I. In the XAML, use the {Binding} markup extension –ElementName: ONLY if the source is a UI element –Path: inside the source, the source property’s name –Alternative syntax C# nyelvű leírás (ritkán használt) –Window konstruktorban az InitializeComponent() után, vagy Loaded eseménykezelőben… 11 <TextBox Name="textBoxToEnable" IsEnabled="{Binding ElementName=checkBoxEnabled, Path=IsChecked}" /> textBoxToEnable.SetBinding(TextBox.IsEnabledProperty, new Binding("IsChecked") { Source = checkBoxEnabled }); "{Binding IsChecked, ElementName=checkBoxEnabled}"

V 1.0ÓE-NIK, 2014 Data Binding I. – defining the Path The full source object should be used (rare): –Path=. Property (inside the source): –ElementName=checkBoxEnabled, Path=IsChecked Property inside the property: –ElementName=listBox, Path=SelectedItem.Name Indexed property inside the property: –ElementName=listBox, Path=Items[0] 12

V 1.0ÓE-NIK, 2014 Data Binding I. – Dependency properties The target property must be a dependency property –Unique in WPF, knows more than the “usual” properties –Most, but not all properties of the UI elements are such properties The values for the dependency properties can be set using a variety of ways: –Data Binding –“Inheritance” of values from parent controls –Animation, Storyboard –Style, resource –System settings (themes, user settings) 13

V 1.0ÓE-NIK, 2014 Data Binding I. – Dependency properties Read the documentation, there is a separate section for „Dependency Property Information” E.g. FrameworkElement.Width: 14

V 1.0ÓE-NIK, 2014 Data Binding I. - DataContext The default source of the data binding –If we do not use ElementName or other sources, then the object in this property is the source –Usually we set it for the whole window It is a dependency property, its value can be “inherited” from the parent control –If we set it for a parent control  we set it for all sub-controls –If we set it for the window  we set it for all controls 15 <Label Content="{Binding Age}"

V 1.0ÓE-NIK, 2014 Data Binding I. – DataContext We can set the DataContext in C# code: –XAML: –Setting the DataContext in C#, Loaded event: 16 <Label Content="{Binding Eletkor}" Szemely sz = new Szemely("Péter", 12, "Magyarország", "Budapest"); stackPanel.DataContext = sz; The red rectangle is the StackPanel

V 1.0ÓE-NIK, 2014 Data Binding I. – DataContext We can specify the whole Source object to use in the data binding –If Path is missing or Path=. –If no source is specified, then the whole DataContext is used 17 public override string ToString() //Person.ToString() { return name + " (" + birthYear + ")"; } public MainWindow() { InitializeComponent(); this.DataContext = new Person() { Name = "Peti", BirthYear = 1985 }; }

V 1.0ÓE-NIK, 2014 Data Binding I. – modes Possible modes: OneWay, TwoWay, OneWayToSource, OneTime, Default 18 <TextBox Text="{Binding Value, ElementName=slider, Mode=TwoWay}"

V 1.0ÓE-NIK, 2014 Data Binding I. – update strategies Used with TwoWay or OneWayToSource modes –LostFocus: this is the default with TextBox.Text –PropertyChanged: usually this is the default –Explicit: we have to call BindingExpression.UpdateSource() –Default 19 <TextBox Text="{Binding Value, ElementName=slider, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

V 1.0ÓE-NIK, 2014 Data Binding – debug NO EXCEPTIONS ARE THROWN!!! We only get notification in the Output window, when the data is actually requested –In the example: bad property names (same error message if the property is there, only it’s not public!) 20

V 1.0ÓE-NIK, 2014 MVC vs MVP vs MVVM Almost the same: We will NOT use pure MVVM (Trigger, Command would also be required) –But we follow the rule that a ViewModel class has to be inserted between the window and the code-behind 21

V 1.0ÓE-NIK, 2014 Exercise 22

V 1.0ÓE-NIK, 2014 Exercise 23

V 1.0ÓE-NIK, 2014 Home work – playlist manager Artist, Title, Length, IsFavourite, Comment Load/Save 24