Professional Documents
Culture Documents
WPF Binding
WPF Binding
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
Agenda
Introducing Binding Working with Type Converters Binding Lists and Data Templates Using Binding and Data Templates
Introducing Binding
Often need to update one element with information from another Display information from collection of objects in list, or combo box Or need to work with data from database In all these cases
Simplest solution is to bind data from binding source to
target
FrameworkElement object
code
Can set Mode property of Binding to control direction of data flow (one/two directions?)
Learn More @ http://www.learnnowonline.com
Copyright by Application Developers Training Company
Value Converters
Value Converter block in figure represents instance of class that implements System.Windows.Data.IValueConverter interface Doing binding declaratively, in XAML, often requires value converter
Select a customer from a ListBox, display combined FirstName
Binding Details
Standard Binding markup extension includes ElementName and Path attributes:
Text=
ElementName=DemoSlider }"
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
Binding Details
Binding Markup extension shortcut for child element syntax:
<TextBox Width="40" Name="DemoTextBox" Height="23"> <TextBox.Text> <Binding ElementName="DemoSlider" Path="Value" /> </TextBox.Text> </TextBox>
Learn More @ http://www.learnnowonline.com
Copyright by Application Developers Training Company
Binding is one-way: Data moves in one direction Set Mode property of Binding to change Set Mode to:
OneTime OneWay
TwoWay
Learn More @ http://www.learnnowonline.com
Copyright by Application Developers Training Company
DEMO
Two-way binding, SimpleBinding3
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
DEMO
SimpleBinding4
Agenda
Introducing Binding Working with Type Converters Binding Lists and Data Templates Using Binding and Data Templates
What happens when source to target requires conversion? You have color name, want to apply to a property that requires a brush?
How can you convert during the binding process?
Learn More @ http://www.learnnowonline.com
Copyright by Application Developers Training Company
DEMO
BindingWithConverter1
StringFormat Details
Simplest format: StringFormat=0.0 Can also specify numeric formatting string, as if calling String.Format method:
StringFormat={}{0:F1} {} escapes formatting string Include text, dont need escape StringFormat=The value is: {0:F1}
More tricks:
Can bind to multiple elements using MultiBinding Check it out!
Converters
Implement IValueConverter interface Requires Convert and ConvertBack methods Parameters:
value (System.Object)
targetType (System.Type)
parameter (System.Object) culture (System.Globalization.Culture)
Converter Warning
Converter doesnt trap exceptions
Treated as runtime errors
DEMO
IntegerToThicknessConverter
Agenda
Introducing Binding Working with Type Converters Binding Lists and Data Templates Using Binding and Data Templates
Imagine scenario:
Want to display list box with numbers
Agenda
Introducing Binding Working with Type Converters Binding Lists and Data Templates Using Binding and Data Templates
Learn More!
This is an excerpt from a larger course. Visit www.learnnowonline.com for the full details! Learn more about WPF on SlideShare:
Intro to Windows Presentation Foundation (WPF) WPF: Advanced Controls