SlideShare uma empresa Scribd logo
1 de 44
Data Binding in WPF Data Binding, Binding Properties ,[object Object],[object Object],[object Object],[object Object],[object Object]
Table of Contents ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Table of Contents (2) ,[object Object],[object Object],[object Object],[object Object],[object Object]
Why We Need Data Binding?
Why We Need Data Binding? ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Why We Need Data Binding? (2) ,[object Object],[object Object],[object Object],[object Object],[object Object]
Simple  B inding
Simple  B inding ,[object Object],[object Object],[object Object]
Simple  B inding  (2) ,[object Object],[object Object],[object Object],[object Object],<TextBox ...> <TextBox.Text> <Binding Path=&quot;SomeName&quot; /> </TextBox.Text> </TextBox> <TextBox Text=&quot;{Binding Path=SomeName}&quot; />
Data Contexts
Data Contexts ,[object Object],[object Object],[object Object],[object Object],[object Object]
Data Contexts (2) ,[object Object],[object Object],<!-- DataContextWindow.xaml --> <Grid Name=&quot;GridMain&quot;> … <TextBlock …>Name: </TextBlock> <TextBox Text=&quot;{Binding Path=Name}&quot; … /> <TextBlock …>Age:</TextBlock> <TextBox Text=&quot;{Binding Path=Age}&quot; … /> <Button Name=&quot;ButtonBirthday Content=&quot;Birthday!&quot; … /> </Grid>
Data Contexts (3) ,[object Object],public partial class MainWindow : Window { Person person = new Person(&quot;Tom&quot;, 11);  public MainWindow() { InitializeComponent(); GridMain.DataContext = person; } … }
Data Contexts Live Demo
Binding to Other Controls
Binding to Other Controls ,[object Object],[object Object],<TextBox Name=&quot;ageTextBox&quot; Foreground=&quot;Red&quot; … /> <Button … Foreground=&quot;{Binding ElementName=ageTextBox,  Path=Foreground}&quot; Content=&quot;Birthday&quot; />
Binding to Other Controls Live Demo
The  Binding  Class and Its Properties
Binding  Class ,[object Object],[object Object],[object Object],[object Object],<TextBox x:Name=&quot;TextBoxPerson&quot;  Foreground=&quot;{Binding Path=Age, Mode=OneWay, Source={StaticResource Tom}, Converter={StaticResource ageConverter}}&quot; />
Binding  Class (2) ,[object Object],[object Object],[object Object],[object Object],[object Object]
Binding  Class (3) ,[object Object],[object Object],[object Object],[object Object]
Value Conversion
Value Conversion ,[object Object],[object Object],[object Object],[object Object],<TextBox Text=&quot;{Binding Path=Age}&quot; Foreground=&quot;{Binding Path=Age, …}&quot; … />
Value Conversion (2) ,[object Object],[object Object],[object Object],[object Object]
Value Conversion (3) ,[object Object],[object Object],[object Object]
Value Conversion (4) ,[object Object],public class AgeToForegroundConverter : IValueConverter { public object Convert(object value,  Type targetType, …) { if (targetType != typeof(Brush)) {  return null;  } int age = int.Parse(value.ToString()); return (age > 25 ? Brushes.Red : Brushes.Black); } … }
Value Conversion (4) ,[object Object],<Window … xmlns:local=&quot;clr-namespace:WithBinding&quot;> <Window.Resources> <local:Person x:Key=&quot;Tom&quot; … /> <local:AgeToForegroundConverter x:Key=&quot;ageConverter&quot;/> </Window.Resources> <Grid DataContext=&quot;{StaticResource Tom}&quot;> … <TextBox Text=&quot;{Binding Path=Age}&quot; Foreground=&quot;{Binding Path=Age, Converter={StaticResource ageConverter}}&quot; … /> … <Button … Foreground=&quot;{Binding Path=Foreground, ElementName=ageTextBox}&quot;>Birthday</Button> </Grid> </Window>
Value Conversion Live Demo
Data Validation
Data Validation ,[object Object],[object Object],class EGNValidationRule : ValidationRule { public override ValidationResult Validate( object value, CultureInfo cultureInfo) { if (Regex.IsMatch((string)value, &quot;{10}&quot;)) return new ValidationResult(true, null); else return new ValidationResult(false, &quot;Invalid EGN&quot;); } }
Data Validation (2) ,[object Object],[object Object],[object Object],[object Object],Validation.GetHasError(UIElement) var errors = Validation.GetErrors(UIElement); string errorMsg = (string)errors[0].ErrorContent;
Data Validation (3) <Window x:Class=&quot;BindingValidation.MainWindow&quot; … xmlns:local=&quot;clr-namespace:BindingValidation&quot; /> … <TextBox Name=&quot;TextBoxEGN&quot;> <TextBox.Text> <Binding Path=&quot;EGN&quot;> <Binding.ValidationRules> <local:EGNValidationRule /> </Binding.ValidationRules> </Binding> </TextBox.Text> </TextBox> … </Window> ,[object Object]
Data Validation (4) <Window.Resources> <Style TargetType=&quot;{x:Type TextBox}&quot;> <Setter Property=&quot;Validation.ErrorTemplate&quot;> <Setter.Value> <ControlTemplate> <WrapPanel> <Border BorderBrush=&quot;Red&quot;> <AdornedElementPlaceholder/> </Border> <TextBlock Foreground=&quot;Red&quot;>!</TextBlock> </WrapPanel> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> ,[object Object]
Binding Path Syntax
Binding Path Syntax ,[object Object],[object Object],[object Object],[object Object]
Binding Path Syntax (2) ,[object Object],[object Object],[object Object],[object Object],[object Object]
R elative  S ources
R elative  S ources ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],<TextBox ... ToolTip=&quot;{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}&quot;>
U pdate  S ource  T riggers
U pdate  S ource  T riggers ,[object Object],[object Object],[object Object],<TextBox …> <TextBox.Text> <Binding Path=&quot;Age&quot;  UpdateSourceTrigger=&quot;PropertyChanged&quot;> … </Binding> </TextBox.Text> </TextBox>
U pdate  S ource  T riggers ,[object Object],[object Object],[object Object],[object Object],[object Object]
Simple Data Binding ,[object Object],http://academy.telerik.com
Exercises ,[object Object],[object Object]
Exercises (2) ,[object Object],[object Object]

Mais conteúdo relacionado

Mais procurados

ADO.NET -database connection
ADO.NET -database connectionADO.NET -database connection
ADO.NET -database connection
Anekwong Yoddumnern
 
ASP.NET Session 11 12
ASP.NET Session 11 12ASP.NET Session 11 12
ASP.NET Session 11 12
Sisir Ghosh
 
Ch06 ado.net fundamentals
Ch06 ado.net fundamentalsCh06 ado.net fundamentals
Ch06 ado.net fundamentals
Madhuri Kavade
 
For Beginners - Ado.net
For Beginners - Ado.netFor Beginners - Ado.net
For Beginners - Ado.net
Tarun Jain
 

Mais procurados (20)

ADO.NET
ADO.NETADO.NET
ADO.NET
 
ADO.NET -database connection
ADO.NET -database connectionADO.NET -database connection
ADO.NET -database connection
 
Ajax
AjaxAjax
Ajax
 
Ado .net
Ado .netAdo .net
Ado .net
 
Chapter 3: ado.net
Chapter 3: ado.netChapter 3: ado.net
Chapter 3: ado.net
 
For Beginers - ADO.Net
For Beginers - ADO.NetFor Beginers - ADO.Net
For Beginers - ADO.Net
 
ASP.NET 08 - Data Binding And Representation
ASP.NET 08 - Data Binding And RepresentationASP.NET 08 - Data Binding And Representation
ASP.NET 08 - Data Binding And Representation
 
ASP.NET Session 11 12
ASP.NET Session 11 12ASP.NET Session 11 12
ASP.NET Session 11 12
 
Diving in the Flex Data Binding Waters
Diving in the Flex Data Binding WatersDiving in the Flex Data Binding Waters
Diving in the Flex Data Binding Waters
 
Ado.net
Ado.netAdo.net
Ado.net
 
Java script
Java scriptJava script
Java script
 
ADO.NET
ADO.NETADO.NET
ADO.NET
 
Ch06 ado.net fundamentals
Ch06 ado.net fundamentalsCh06 ado.net fundamentals
Ch06 ado.net fundamentals
 
Introduction to ADO.NET
Introduction to ADO.NETIntroduction to ADO.NET
Introduction to ADO.NET
 
Data Binding
Data BindingData Binding
Data Binding
 
For Beginners - Ado.net
For Beginners - Ado.netFor Beginners - Ado.net
For Beginners - Ado.net
 
AJAX
AJAXAJAX
AJAX
 
Chap14 ado.net
Chap14 ado.netChap14 ado.net
Chap14 ado.net
 
Ado.Net Tutorial
Ado.Net TutorialAdo.Net Tutorial
Ado.Net Tutorial
 
Visual Basic.Net & Ado.Net
Visual Basic.Net & Ado.NetVisual Basic.Net & Ado.Net
Visual Basic.Net & Ado.Net
 

Semelhante a Simple Data Binding

Silverlight week5
Silverlight week5Silverlight week5
Silverlight week5
iedotnetug
 
Ado.Net Data Services (Astoria)
Ado.Net Data Services (Astoria)Ado.Net Data Services (Astoria)
Ado.Net Data Services (Astoria)
Igor Moochnick
 
Data binding in silverlight
Data binding in silverlightData binding in silverlight
Data binding in silverlight
msarangam
 
Data binding in silverlight
Data binding in silverlightData binding in silverlight
Data binding in silverlight
msarangam
 
Flex3 data binding
Flex3 data bindingFlex3 data binding
Flex3 data binding
guestdf3003
 
Xhtml Part2
Xhtml Part2Xhtml Part2
Xhtml Part2
nleesite
 

Semelhante a Simple Data Binding (20)

Dev308
Dev308Dev308
Dev308
 
Accessing loosely structured data from F# and C#
Accessing loosely structured data from F# and C#Accessing loosely structured data from F# and C#
Accessing loosely structured data from F# and C#
 
Silverlight week5
Silverlight week5Silverlight week5
Silverlight week5
 
Entity Framework v2 Best Practices
Entity Framework v2 Best PracticesEntity Framework v2 Best Practices
Entity Framework v2 Best Practices
 
Intake 38 data access 5
Intake 38 data access 5Intake 38 data access 5
Intake 38 data access 5
 
Silverlight 5 whats new overview
Silverlight 5 whats new overviewSilverlight 5 whats new overview
Silverlight 5 whats new overview
 
Ado.Net Data Services (Astoria)
Ado.Net Data Services (Astoria)Ado.Net Data Services (Astoria)
Ado.Net Data Services (Astoria)
 
Data binding in silverlight
Data binding in silverlightData binding in silverlight
Data binding in silverlight
 
Data binding in silverlight
Data binding in silverlightData binding in silverlight
Data binding in silverlight
 
Wpf Introduction
Wpf IntroductionWpf Introduction
Wpf Introduction
 
Entity framework 4.0
Entity framework 4.0Entity framework 4.0
Entity framework 4.0
 
Intake 37 ef2
Intake 37 ef2Intake 37 ef2
Intake 37 ef2
 
An Overview of Entity Framework
An Overview of Entity FrameworkAn Overview of Entity Framework
An Overview of Entity Framework
 
Silverlight 2 for Developers - TechEd New Zealand 2008
Silverlight 2 for Developers - TechEd New Zealand 2008Silverlight 2 for Developers - TechEd New Zealand 2008
Silverlight 2 for Developers - TechEd New Zealand 2008
 
WPF & Silverlight UI
WPF & Silverlight UIWPF & Silverlight UI
WPF & Silverlight UI
 
Flex3 data binding
Flex3 data bindingFlex3 data binding
Flex3 data binding
 
Overview of entity framework by software outsourcing company india
Overview of entity framework by software outsourcing company indiaOverview of entity framework by software outsourcing company india
Overview of entity framework by software outsourcing company india
 
Xhtml Part2
Xhtml Part2Xhtml Part2
Xhtml Part2
 
Data Bondage in WPF
Data Bondage in WPFData Bondage in WPF
Data Bondage in WPF
 
Winforms
WinformsWinforms
Winforms
 

Mais de Doncho Minkov

Mais de Doncho Minkov (20)

Web Design Concepts
Web Design ConceptsWeb Design Concepts
Web Design Concepts
 
Web design Tools
Web design ToolsWeb design Tools
Web design Tools
 
HTML 5
HTML 5HTML 5
HTML 5
 
HTML 5 Tables and Forms
HTML 5 Tables and FormsHTML 5 Tables and Forms
HTML 5 Tables and Forms
 
CSS Overview
CSS OverviewCSS Overview
CSS Overview
 
CSS Presentation
CSS PresentationCSS Presentation
CSS Presentation
 
CSS Layout
CSS LayoutCSS Layout
CSS Layout
 
CSS 3
CSS 3CSS 3
CSS 3
 
Adobe Photoshop
Adobe PhotoshopAdobe Photoshop
Adobe Photoshop
 
Slice and Dice
Slice and DiceSlice and Dice
Slice and Dice
 
Introduction to XAML and WPF
Introduction to XAML and WPFIntroduction to XAML and WPF
Introduction to XAML and WPF
 
WPF Layout Containers
WPF Layout ContainersWPF Layout Containers
WPF Layout Containers
 
WPF Templating and Styling
WPF Templating and StylingWPF Templating and Styling
WPF Templating and Styling
 
WPF Graphics and Animations
WPF Graphics and AnimationsWPF Graphics and Animations
WPF Graphics and Animations
 
Model View ViewModel
Model View ViewModelModel View ViewModel
Model View ViewModel
 
Introduction to Cross-platform Mobile Development Course
Introduction to Cross-platform Mobile Development CourseIntroduction to Cross-platform Mobile Development Course
Introduction to Cross-platform Mobile Development Course
 
HTML Fundamentals
HTML FundamentalsHTML Fundamentals
HTML Fundamentals
 
Tables and Forms in HTML
Tables and Forms in HTMLTables and Forms in HTML
Tables and Forms in HTML
 
HTML5
HTML5HTML5
HTML5
 
CSS Part I
CSS Part ICSS Part I
CSS Part I
 

Último

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Último (20)

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 

Simple Data Binding

Notas do Editor

  1. * 02/25/12 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  2. * 02/25/12 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  3. * 02/25/12 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  4. * 02/25/12 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  5. * 02/25/12 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  6. * 02/25/12 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  7. * 02/25/12 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  8. * 02/25/12 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  9. * 02/25/12 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  10. * 02/25/12 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  11. * 02/25/12 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  12. * 02/25/12 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  13. * 02/25/12 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  14. * 02/25/12 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  15. * 02/25/12 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  16. * 02/25/12 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  17. * 02/25/12 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  18. * 02/25/12 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##