• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
WPF Binding
 

WPF Binding

on

  • 630 views

Learn to use Binding objects to bind data sources and targets, add data converters to manage conversion during the binding process, and use data templates to modify the layout of bound data in lists.

Learn to use Binding objects to bind data sources and targets, add data converters to manage conversion during the binding process, and use data templates to modify the layout of bound data in lists.

Statistics

Views

Total Views
630
Views on SlideShare
628
Embed Views
2

Actions

Likes
1
Downloads
0
Comments
0

1 Embed 2

https://twitter.com 2

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n

WPF Binding WPF Binding Presentation Transcript

  • WPF: Binding Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Objectives Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Objectives• Learn to use Binding objects to bind data sources and targets Learn More @ http://www.learnnowonline.com Copyright © 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 Learn More @ http://www.learnnowonline.com Copyright © 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 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Agenda Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Agenda• Introducing Binding Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Agenda• Introducing Binding• Working with Type Converters Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Agenda• Introducing Binding• Working with Type Converters• Binding Lists and Data Templates Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Agenda• Introducing Binding• Working with Type Converters• Binding Lists and Data Templates• Using Binding and Data Templates Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Introducing Binding Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Introducing Binding• Often need to update one element with information from another Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Introducing Binding• Often need to update one element with information from another• Display information from collection of objects in list, or combo box Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 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 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 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 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 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 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 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• In other words—need some way to perform binding Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • When to Use Binding? Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • When to Use Binding?• Display in the number of characters in a text box, in a TextBlock control Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • When to Use Binding?• Display in the number of characters in a text box, in a TextBlock control• Display the value of Slider control in a TextBlock Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • When to Use Binding?• Display in 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 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • When to Use Binding?• Display in 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 customer’s photo in Image control Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • When to Use Binding?• Display in 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 customer’s photo in Image control• Display and edit customer’s name in TextBox Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Connecting Sources andTargets Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Connecting Sources andTargets• Every time you use binding Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Connecting Sources andTargets• Every time you use binding • Must supply source for data, and target Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Connecting Sources andTargets• Every time you use binding • Must supply source for data, and target • Normally, target is dependency property of some user interface element Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Connecting Sources andTargets• Every time you use binding • Must supply source for data, and target • Normally, target is dependency property of some user interface element Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Connecting Sources andTargets Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Connecting Sources andTargets• Binding source can be any property of any CLR object Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Connecting Sources andTargets• Binding source can be any property of any CLR object• Binding object connects source to binding target Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Connecting Sources andTargets• Binding source can be any property of any CLR object• Binding object connects source to binding target • Must be dependency property of some FrameworkElement object Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Under the Hood Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Under the Hood• When using binding in XAML Learn More @ http://www.learnnowonline.com Copyright © 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 Learn More @ http://www.learnnowonline.com Copyright © 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 Learn More @ http://www.learnnowonline.com Copyright © 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 Learn More @ http://www.learnnowonline.com Copyright © 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 Learn More @ http://www.learnnowonline.com Copyright © 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 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Value Converters 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 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 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 and LastName fields in TextBlock 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 and LastName fields in TextBlock • Select an integer, bind to BorderThickness of Border 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 and LastName fields in TextBlock • Select an integer, bind to BorderThickness of Border • Can’t bind directly—BorderThickness is a Thickness Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • A Few Simple Examples Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • A Few Simple Examples• DEMO Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Binding Details Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Binding Details• Standard Binding markup extension includes ElementName and Path attributes: Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Binding Details• Standard Binding markup extension includes ElementName and Path attributes: • Text= "{Binding ElementName=DemoSlider, Path=Value}" Learn More @ http://www.learnnowonline.com Copyright © 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: Learn More @ http://www.learnnowonline.com Copyright © 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 }" Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Binding Details Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Binding Details• Path property can refer to property, or property of a property, or indexed property Learn More @ http://www.learnnowonline.com Copyright © 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? Learn More @ http://www.learnnowonline.com Copyright © 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 Learn More @ http://www.learnnowonline.com Copyright © 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) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Binding Details Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Binding Details• Binding Markup extension shortcut for child element syntax: Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 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
  • Setting the Binding Mode Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Setting the Binding Mode• In previous demo, data moves from Slider to TextBox Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Setting the Binding Mode• In previous demo, data moves from Slider to TextBox • Changes you make in TextBox have no effect on slider Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Setting the Binding Mode• In previous demo, data moves from Slider to TextBox • Changes you make in TextBox have no effect on slider• Binding is one-way: Data moves in one direction Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Setting the Binding Mode• In previous demo, data moves from Slider to TextBox • Changes you make in TextBox have no effect on slider• Binding is one-way: Data moves in one direction• Set Mode property of Binding to change Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Setting the Binding Mode• In previous demo, data moves from Slider to TextBox • Changes you make in TextBox have no effect on slider• Binding is one-way: Data moves in one direction• Set Mode property of Binding to change• Set Mode to: Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Setting the Binding Mode• In previous demo, data moves from Slider to TextBox • Changes you make in TextBox have no effect on slider• Binding is one-way: Data moves in one direction• Set Mode property of Binding to change• Set Mode to: • OneTime Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Additional Binding Mode Options(WPF) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Additional Binding Mode Options(WPF)• Can also set Mode to: Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Additional Binding Mode Options(WPF)• Can also set Mode to: • OneWayToSource Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Additional Binding Mode Options(WPF)• Can also set Mode to: • OneWayToSource • Default Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • DEMO Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • DEMO• Two-way binding, SimpleBinding3 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • A Simple Example Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • A Simple Example• Enter text into TextBox, update TextBlock with length of the text Learn More @ http://www.learnnowonline.com Copyright © 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 Learn More @ http://www.learnnowonline.com Copyright © 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 Learn More @ http://www.learnnowonline.com Copyright © 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 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • DEMO Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • DEMO• SimpleBinding4 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Agenda Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Agenda• Introducing Binding Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Agenda• Introducing Binding• Working with Type Converters Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Agenda• Introducing Binding• Working with Type Converters• Binding Lists and Data Templates Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Agenda• Introducing Binding• Working with Type Converters• Binding Lists and Data Templates• Using Binding and Data Templates Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Working with TypeConverters Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Working with TypeConverters• Every example so far moved data from source to target without modifying data Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Working with TypeConverters• Every example so far moved data from source to target without modifying data • Or relied on built-in type converters Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Working with TypeConverters• Every example so far moved data from source to target without modifying data • Or relied on built-in type converters• What happens when source to target requires conversion? Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Working with TypeConverters• Every example so far moved data from source to target without modifying data • Or relied on built-in type converters• What happens when source to target requires conversion?• You have color name, want to apply to a property that requires a brush? Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Working with TypeConverters• Every example so far moved data from source to target without modifying data • Or relied on built-in type converters• 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? More @ http://www.learnnowonline.com Learn Copyright © by Application Developers Training Company
  • Type Converters to theRescue Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Type Converters to theRescue• Type converter injects itself into the binding process Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Type Converters to theRescue• Type converter injects itself into the binding process • Converts data from the source to the target type Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Type Converters to theRescue• Type converter injects itself into the binding process • Converts data from the source to the target type• Must implement System.Windows.Data.IValueConverter interface Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Type Converters to theRescue• Type converter injects itself into the binding process • Converts data from the source to the target type• Must implement System.Windows.Data.IValueConverter interface • Two methods: Convert and ConvertBack Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Type Converters to theRescue• Type converter injects itself into the binding process • Converts data from the source to the target type• Must implement System.Windows.Data.IValueConverter interface • Two methods: Convert and ConvertBack• Simple to create converter, in general Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Using the StringFormatProperty Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Using the StringFormatProperty• Many conversions consist of formatting a string Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Using the StringFormatProperty• Many conversions consist of formatting a string• Could create a type converter; not necessary Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Using the StringFormatProperty• Many conversions consist of formatting a string• Could create a type converter; not necessary• Convert a value into a formatted string? Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Using the StringFormatProperty• Many conversions consist of formatting a string• Could create a type converter; not necessary• Convert a value into a formatted string? • Can often use StringFormat property Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • DEMO Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • DEMO• BindingWithConverter1 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • StringFormat Details Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • StringFormat Details• Simplest format: StringFormat=0.0 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • StringFormat Details• Simplest format: StringFormat=0.0• Can also specify numeric formatting string, as if calling String.Format method: Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • StringFormat Details• Simplest format: StringFormat=0.0• Can also specify numeric formatting string, as if calling String.Format method: • StringFormat={}{0:F1} Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 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 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 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, don’t need escape Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 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, don’t need escape • StringFormat=The value is: {0:F1} Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 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, don’t need escape • StringFormat=The value is: {0:F1}• More tricks: Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 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, don’t need escape • StringFormat=The value is: {0:F1}• More tricks: • Can bind to multiple elements using MultiBinding Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 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, don’t need escape • StringFormat=The value is: {0:F1}• More tricks: • Can bind to multiple elements using MultiBinding • Check it out! Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Using a Type Converter Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Using a Type Converter• Sometimes, StringFormat can’t do the job Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Using a Type Converter• Sometimes, StringFormat can’t do the job • Will need to run code as data transfers Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Using a Type Converter• Sometimes, StringFormat can’t do the job • Will need to run code as data transfers • Need a type converter Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Using a Type Converter• Sometimes, StringFormat can’t do the job • Will need to run code as data transfers • Need a type converter• Determine source and target data types Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Using a Type Converter• Sometimes, StringFormat can’t do the job • Will need to run code as data transfers • Need a type converter• Determine source and target data types • Write code to perform the conversion Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Using a Type Converter• Sometimes, StringFormat can’t do the job • Will need to run code as data transfers • Need 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) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Using a Type Converter• Sometimes, StringFormat can’t do the job • Will need to run code as data transfers • Need 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 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Using a Type Converter• Sometimes, StringFormat can’t do the job • Will need to run code as data transfers • Need 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 • What’s the problem? Thickness structure has four Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Creating the Type Converter Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Creating the Type Converter• Attempt to bind SelectedValue of ComboBox to BorderThickness of Border, and fails Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Creating the Type Converter• Attempt to bind SelectedValue of ComboBox to BorderThickness of Border, and fails• Need type (or value) converter Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Converters Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Converters• Implement IValueConverter interface Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Converters• Implement IValueConverter interface• Requires Convert and ConvertBack methods Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Converters• Implement IValueConverter interface• Requires Convert and ConvertBack methods• Parameters: Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Converters• Implement IValueConverter interface• Requires Convert and ConvertBack methods• Parameters: • value (System.Object) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Converters• Implement IValueConverter interface• Requires Convert and ConvertBack methods• Parameters: • value (System.Object) • targetType (System.Type) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Converters• Implement IValueConverter interface• Requires Convert and ConvertBack methods• Parameters: • value (System.Object) • targetType (System.Type) • parameter (System.Object) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Converters• Implement IValueConverter interface• Requires Convert and ConvertBack methods• Parameters: • value (System.Object) • targetType (System.Type) • parameter (System.Object) • culture (System.Globalization.Culture) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Converter Warning Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Converter Warning• Converter doesn’t trap exceptions Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Converter Warning• Converter doesn’t trap exceptions • Treated as runtime errors Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Converter Warning• Converter doesn’t trap exceptions • Treated as runtime errors• You must trap and handle all runtime errors Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Converter Warning• Converter doesn’t trap exceptions • Treated as runtime errors• You must trap and handle all runtime errors • Return DependencyProperty.UnsetValue on error Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Converter Warning• Converter doesn’t 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 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Converter Warning• Converter doesn’t 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! Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • DEMO Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • DEMO• IntegerToThicknessConverter Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Referencing the TypeConverter Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Referencing the TypeConverter• Need way to refer to type converter in XAML Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Referencing the TypeConverter• Need way to refer to type converter in XAML• Like any other resource, declare instance in resources; UserControl.Resources in demo Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Referencing the TypeConverter• Need way to refer to type converter in XAML• Like any other resource, declare instance in resources; UserControl.Resources in demo• Need namespace reference Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Referencing the TypeConverter• Need way to refer to type converter in XAML• Like any other resource, declare instance in resources; UserControl.Resources in demo• Need namespace reference • xmlns:local="clr-namespace:Binding" Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Referencing the TypeConverter• Need way to refer to type converter in XAML• Like any other resource, declare instance in resources; UserControl.Resources in demo• Need namespace reference • xmlns:local="clr-namespace:Binding"• Then, within UserControl.Resources: Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Referencing the TypeConverter• Need way to refer to type converter in XAML• Like any other resource, declare instance in resources; UserControl.Resources in demo• Need namespace reference • xmlns:local="clr-namespace:Binding"• Then, within UserControl.Resources: • <UserControl.Resources> <local:IntegerToThicknessConverter x:Key="thicknessConverter" /> Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Using the Type Converter Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Using the Type Converter• Add a setting in Binding markup extension Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Using the Type Converter• Add a setting in Binding markup extension <Border BorderThickness="{Binding ElementName=ThicknessComboBox, Path=SelectedValue, Converter={StaticResource thicknessConverter}}" BorderBrush="Black" Margin="5"> Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Using the Visual StudioDesigner Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Using the Visual StudioDesigner• DEMO Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Agenda Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Agenda• Introducing Binding Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Agenda• Introducing Binding• Working with Type Converters Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Agenda• Introducing Binding• Working with Type Converters• Binding Lists and Data Templates Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Agenda• Introducing Binding• Working with Type Converters• Binding Lists and Data Templates• Using Binding and Data Templates Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Binding Lists and DataTemplates Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Binding Lists and DataTemplates• Common use of binding: Display data in lists Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Binding Lists and DataTemplates• Common use of binding: Display data in lists • ComboBox/ListBox Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Binding Lists and DataTemplates• Common use of binding: Display data in lists • ComboBox/ListBox• Whether retrieving data from database, or using a collection in memory Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Binding Lists and DataTemplates• Common use of binding: Display data in lists • ComboBox/ListBox• Whether retrieving data from database, or using a collection in memory • Useful to be able to display data Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Binding Lists and DataTemplates• Common use of binding: Display data in lists • ComboBox/ListBox• Whether retrieving data from database, or using a collection in memory • Useful to be able to display data• Imagine scenario: Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Binding Lists and DataTemplates• Common use of binding: Display data in lists • ComboBox/ListBox• Whether retrieving data from database, or using a collection in memory • Useful to be able to display data• Imagine scenario: • Want to display list box with numbers Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Binding Lists and DataTemplates• Common use of binding: Display data in lists • ComboBox/ListBox• Whether retrieving data from database, or using a collection in memory • Useful to be able to display data• Imagine scenario: • Want to display list box with numbers • Also display bar with magnitude Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Binding Lists and DataTemplates• Common use of binding: Display data in lists • ComboBox/ListBox• Whether retrieving data from database, or using a collection in memory • Useful to be able to display data• Imagine scenario: • Want to display list box with numbers • Also display bar with magnitude Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Filling the List/Displaying theData Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Filling the List/Displaying theData• DEMO Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Adding a Data Template Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Adding a Data Template• Given ListBox displaying numbers Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Adding a Data Template• Given ListBox displaying numbers • Next task is to modify layout for each item in list Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Adding a Data Template• Given ListBox displaying numbers • Next task is to modify layout for each item in list• XML Data Template designed just for this Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Adding a Data Template• Given ListBox displaying numbers • Next task is to modify layout for each item in list• XML Data Template designed just for this • Allows you to pass one row of data at a time through custom format, as control renders output Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Adding a Data Template• Given ListBox displaying numbers • Next task is to modify layout for each item in list• XML Data Template designed just for this • Allows you to pass one row of data at a time through custom format, as control renders output• Data template simply a chunk of XAML markup Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Adding a Data Template• Given ListBox displaying numbers • Next task is to modify layout for each item in list• XML Data Template designed just for this • Allows you to pass one row of data at a time through custom format, as control renders output• Data template simply a chunk of XAML markup • Defines how each row should be displayed Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Adding a Data Template• Given ListBox displaying numbers • Next task is to modify layout for each item in list• XML Data Template designed just for this • Allows you to pass one row of data at a time through custom format, as control renders output• Data template simply a chunk of XAML markup • Defines how each row should be displayed • Can contain any markup you like Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Adding a Data Template• Given ListBox displaying numbers • Next task is to modify layout for each item in list• XML Data Template designed just for this • Allows you to pass one row of data at a time through custom format, as control renders output• Data template simply a chunk of XAML markup • Defines how each row should be displayed • Can contain any markup you like • Include at least one Binding markup extension Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Adding a Data Template• Given ListBox displaying numbers • Next task is to modify layout for each item in list• XML Data Template designed just for this • Allows you to pass one row of data at a time through custom format, as control renders output• Data template simply a chunk of XAML markup • Defines how each row should be displayed • Can contain any markup you like • Include at least one Binding markup extension • Otherwise, pretty dull Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Agenda Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Agenda• Introducing Binding Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Agenda• Introducing Binding• Working with Type Converters Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Agenda• Introducing Binding• Working with Type Converters• Binding Lists and Data Templates Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Agenda• Introducing Binding• Working with Type Converters• Binding Lists and Data Templates• Using Binding and Data Templates Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Using Binding and DataTemplates Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Using Binding and DataTemplates • A final scenario that uses binding and data templates Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Using Binding and DataTemplates • A final scenario that uses binding and data templates • Display list of color names and color “swatches” Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Using Binding and DataTemplates • A final scenario that uses binding and data templates • Display list of color names and color “swatches” • Select a color and set background color for border Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Learn More! Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Learn More!• This is an excerpt from a larger course. Visit www.learnnowonline.com for the full details! Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Learn More!• This is an excerpt from a larger course. Visit www.learnnowonline.com for the full details! Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Learn More!• This is an excerpt from a larger course. Visit www.learnnowonline.com for the full details!• Learn more about WPF on SlideShare: Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 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) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 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 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company