windows 8 binding
TRANSCRIPT
-
7/30/2019 Windows 8 Binding
1/28
Learn More @ http://www.learnnowonline.comCopyright by Application Developers Training Company
Windows 8 Binding Part 1
http://www.LearnNowOnline.com
http://www.learnnowonline.com/http://www.learnnowonline.com/ -
7/30/2019 Windows 8 Binding
2/28
Learn More @ http://www.learnnowonline.comCopyright by Application Developers Training Company
Objectives
Learn to use Binding objects to bind data
sources and targets
Add data converters to manage conversion
during the binding process
Use data templates to modify the layout of
bound data in lists
http://www.learnnowonline.com/http://www.learnnowonline.com/ -
7/30/2019 Windows 8 Binding
3/28
Learn More @ http://www.learnnowonline.comCopyright by Application Developers Training Company
Agenda
Introducing Binding
Working with Type Converters
Binding Lists and Data Templates Using Binding and Data Templates
http://www.learnnowonline.com/http://www.learnnowonline.com/ -
7/30/2019 Windows 8 Binding
4/28
Learn More @ http://www.learnnowonline.comCopyright by Application Developers Training Company
When to Use Binding?
Display the number of characters in a text box,
in a TextBlock control
Display the value of Slider control in a TextBlock
Display list of customers in ListBox
Display customers photo in Image control
Display and edit customers name in TextBox
Of course, there are an infinite number of
reasons
http://www.learnnowonline.com/http://www.learnnowonline.com/ -
7/30/2019 Windows 8 Binding
5/28
Learn More @ http://www.learnnowonline.comCopyright by Application Developers Training Company
Connecting Sources and Targets
Every time you use binding
Must supply source for data, and target
Normally, target is dependency property of some
user interface element
http://www.learnnowonline.com/http://www.learnnowonline.com/ -
7/30/2019 Windows 8 Binding
6/28
Learn More @ http://www.learnnowonline.comCopyright by Application Developers Training Company
Connecting Sources and Targets
Binding source: any property of any object
Binding object connects source to binding target
Must be dependency property of some
DependencyObject instance
http://www.learnnowonline.com/http://www.learnnowonline.com/ -
7/30/2019 Windows 8 Binding
7/28
Learn More @ http://www.learnnowonline.comCopyright by Application Developers Training Company
Under the Hood
When using binding in XAML Create instance of Binding class, setting various
properties that affect its behavior
XAML provides binding markup extension Hides this fact, but still working with Binding object
Completely transparent if you create Binding object
in code
Can set Mode property of Binding to control
direction of data flow (one/two directions?)
http://www.learnnowonline.com/http://www.learnnowonline.com/ -
7/30/2019 Windows 8 Binding
8/28
Learn More @ http://www.learnnowonline.comCopyright by Application Developers Training Company
Value Converters
Value Converter provides instance of class that
implements
Windows.UI.Xaml.Data.IValueConverter
interface
Binding declaratively often requires value
converter
http://www.learnnowonline.com/http://www.learnnowonline.com/ -
7/30/2019 Windows 8 Binding
9/28
Learn More @ http://www.learnnowonline.comCopyright by Application Developers Training Company
Value Converters
Select a customer from a ListBox, displaycombined FirstName and LastName fields in
TextBlock
Select an integer, bind to BorderThickness ofBorder
Cant bind directlyBorderThickness is a Thickness
structure Infinite reasons to use a value converter
But only if binding declaratively
http://www.learnnowonline.com/http://www.learnnowonline.com/ -
7/30/2019 Windows 8 Binding
10/28
Learn More @ http://www.learnnowonline.comCopyright by Application Developers Training Company
A Few Simple Examples
SimpleBinding1
SimpleBinding2
http://www.learnnowonline.com/http://www.learnnowonline.com/ -
7/30/2019 Windows 8 Binding
11/28
Learn More @ http://www.learnnowonline.comCopyright by Application Developers Training Company
Binding Details
Standard Binding markup extension includes
ElementName and Path attributes:
Text="{Binding ElementName=DemoSlider,
Path=Value}"
Path attribute not required:
Text="{Binding Value,
ElementName=DemoSlider}"
Choose whichever you like
http://www.learnnowonline.com/http://www.learnnowonline.com/ -
7/30/2019 Windows 8 Binding
12/28
Learn More @ http://www.learnnowonline.comCopyright by Application Developers Training Company
Binding Details
Path property can refer to property, or
property of a property, or indexed property
Need to refer to an attached property?
Grid.Row, for example
Set Path property to (Grid.Row) (in parentheses)
Parentheses force evaluationwont work without
Parentheses not necessary for property of a property
http://www.learnnowonline.com/http://www.learnnowonline.com/ -
7/30/2019 Windows 8 Binding
13/28
Learn More @ http://www.learnnowonline.comCopyright by Application Developers Training Company
Binding Details
Binding Markup extension shortcut for child
element syntax:
http://www.learnnowonline.com/http://www.learnnowonline.com/ -
7/30/2019 Windows 8 Binding
14/28
Learn More @ http://www.learnnowonline.comCopyright by Application Developers Training Company
Setting the Binding Mode
In demo, data moves from Slider to TextBox
Changes in TextBox have no effect on slider
Binding is one-way: Data moves in one
direction
Set Mode property of Binding to change
OneTime
OneWay
TwoWay
http://www.learnnowonline.com/http://www.learnnowonline.com/ -
7/30/2019 Windows 8 Binding
15/28
Learn More @ http://www.learnnowonline.comCopyright by Application Developers Training Company
DEMO
Two-way binding, SimpleBinding3
http://www.learnnowonline.com/http://www.learnnowonline.com/ -
7/30/2019 Windows 8 Binding
16/28
Learn More @ http://www.learnnowonline.comCopyright by Application Developers Training Company
A Simple Example
Enter text into TextBox, update TextBlock with
length of the text
Could react to TextChanged event of TextBox
Better: Bind Text property of TextBlock to
Text.Length property of TextBox
Points out that binding can use expressions
http://www.learnnowonline.com/http://www.learnnowonline.com/ -
7/30/2019 Windows 8 Binding
17/28
Learn More @ http://www.learnnowonline.comCopyright by Application Developers Training Company
DEMO
SimpleBinding4
http://www.learnnowonline.com/http://www.learnnowonline.com/ -
7/30/2019 Windows 8 Binding
18/28
Learn More @ http://www.learnnowonline.comCopyright by Application Developers Training Company
Agenda
Introducing Binding
Working with Type Converters
Binding Lists and Data Templates Using Binding and Data Templates
http://www.learnnowonline.com/http://www.learnnowonline.com/ -
7/30/2019 Windows 8 Binding
19/28
Learn More @ http://www.learnnowonline.comCopyright by Application Developers Training Company
Using a Type Converter
Determine source and target data types
Write code to perform the conversion
Perhaps write code to convert back (for two-way
binding)
Sample binds integer from combo box to
BorderThickness property
Whats the problem? Thickness structure has four
values
http://www.learnnowonline.com/http://www.learnnowonline.com/ -
7/30/2019 Windows 8 Binding
20/28
Learn More @ http://www.learnnowonline.comCopyright by Application Developers Training Company
Creating the Type Converter
Attempt to bind SelectedValue of ComboBox
to BorderThickness of Border, and doesnt
work
Need type (or value) converter
http://www.learnnowonline.com/http://www.learnnowonline.com/ -
7/30/2019 Windows 8 Binding
21/28
Learn More @ http://www.learnnowonline.comCopyright by Application Developers Training Company
Converters
Implement IValueConverter interface
Requires Convert and ConvertBack methods
Parameters: value (System.Object)
targetType (System.Type)
parameter (System.Object)
Language (System.String)
http://www.learnnowonline.com/http://www.learnnowonline.com/ -
7/30/2019 Windows 8 Binding
22/28
Learn More @ http://www.learnnowonline.comCopyright by Application Developers Training Company
Handle Null Case
Always possible that value will be null
Code runs at design time!
Always verify that value isnt null before
performing conversion
http://www.learnnowonline.com/http://www.learnnowonline.com/ -
7/30/2019 Windows 8 Binding
23/28
Learn More @ http://www.learnnowonline.comCopyright by Application Developers Training Company
Converter Warning
Converter doesnt trap exceptions
Treated as runtime errors
You must trap and handle all runtime errors
Return DependencyProperty.UnsetValue on error
Not handled in this simple demo
Worth considering!
http://www.learnnowonline.com/http://www.learnnowonline.com/ -
7/30/2019 Windows 8 Binding
24/28
Learn More @ http://www.learnnowonline.comCopyright by Application Developers Training Company
DEMO
IntegerToThicknessConverter
http://www.learnnowonline.com/http://www.learnnowonline.com/ -
7/30/2019 Windows 8 Binding
25/28
Learn More @ http://www.learnnowonline.comCopyright by Application Developers Training Company
Referencing the Type Converter
Need way to refer to type converter in XAML
Like any other resource, declare instance in
resources; Page.Resources in demo
Need namespace reference
xmlns:local="using:Binding"
Then, within Page.Resources:
http://www.learnnowonline.com/http://www.learnnowonline.com/ -
7/30/2019 Windows 8 Binding
26/28
Learn More @ http://www.learnnowonline.comCopyright by Application Developers Training Company
Using the Type Converter
Add a setting in Binding markup extension
http://www.learnnowonline.com/http://www.learnnowonline.com/ -
7/30/2019 Windows 8 Binding
27/28
Learn More @ http://www.learnnowonline.comCopyright by Application Developers Training Company
Using the Visual Studio Designer
DEMO
http://www.learnnowonline.com/http://www.learnnowonline.com/ -
7/30/2019 Windows 8 Binding
28/28
Learn More @ http://www.learnnowonline.comC i h b A li i D l T i i C
End of Part 1
http://www.LearnNowOnline.com
http://www.learnnowonline.com/http://www.learnnowonline.com/