SlideShare una empresa de Scribd logo
1 de 21
Descargar para leer sin conexión
Silverlight 4 Course Introduction to Silverlight  Layout  Input Handling  Applications, Resources, Deployment Data Binding, View Model Out of Browser, File Access, Printing WCF RIA Services (4 Weeks )
Silverlight Week 5 Agenda  Binding Data contexts Data Templates Binding to collections MVVM ClassProject
Message from Silverlight :
What is Data ?
What is Data ?
What is Data ? Database Table. XML  In Memory Object Collections Web Service
Data in Silverlight Silverlight is client side technology. You do not connect directly to the database table. Using some sort of service you query and get the data. Data comes in as XML, JSON etc. You convert it into some object or collection of objects. Bind UI to the object property.
Data Binding
Data Binding Data could be Database, In memory objects, XML. Binding UI to object. In Silverlight binding could be done from one element’s property to another element’s property. A communication channel opens between the 2. Binding binding = new Binding(); binding.Source = person; binding.Path = new PropertyPath(“Age”); txtblock.SetBinding(TextBlock.TextProperty, binding); What if several property of same source needs to be bound ?
Binding in Code Behind FrameworkElement’s SetBinding() : Any UI element derived from FrameworkElement. UI element is the target. BindingOperations.SetBinding() : Static method to support DependencyObject. Target specified as first parameter.
Binding in XAML Binding Expressions uses Markup Extensions. <TextBox Text=“{Binding Path=Surname}” /> Text is the target property whereas Surname is the source property. Which object’s surname property to use ? Determined by DataContext.
Data Contexts All UIElements have a property called “DataContext”. Property Value Inheritance helps us to propagate the DataContext set on the Root Element. PVI is equivalent to Ambient Property in Windows Forms. Data Binding Mode specifies  the communication mode. One way (Silverlight Default) 2 way (WPF Default)  One Time ChangeNotification is the mechanism used by Source Property. INotifyChanged Interface.
Demo Create a Person object with properties : Firstname (String) Lastname (String) Age (int) MainPage create a person Person src = new Person { FirstName = “John”, LastName = “Doe”, Age = 25 };  Set data context in constructor : this.DataContext = src XAML : <TextBox Text=“{Binding Path=FirstName}” /> Modify the age. No effect. Explain binding mode. Add a button to modify age. UI textbox is not updated. Why ? Explain INotify Later implement INotifyPropertyChanged interface.
Clearing Binding BindingOperations.ClearBinding(txtblock, TextBlock.Textproperty) BindingOperations.ClearAllBinding(txtblock)
Data Template How to present data in a UI.  Used by ItemsControls like ListBox and ContentControls like Buttons. Data Templates are just XAML markup with Binding expression.
Demo Data template Part1 Move the grid to <UserControl.Resources> <DataTemplate x:Key=“myTemplate”> Add ContentControl Binding to collection Part2 Change to List of persons Use a ListBox.
Collections Binding INotifyCollectionChanged ObservableCollection<T> Demo : Add a button and add new item to collection. UI not updated. Why ? br />Change List to ObservableCollection.
MVVM What is it trying to solve ? Often Code behind has validation logic, interaction logic, application logic. Tightly coupled which causes issues: Less maintainability Less manageability Testing issues Ex : ComboBox selected item.
Various Models MVC : Model View Controller MVP : Model View Presenter Presentation Model : Martin Fowler MVVM : Model View ViewModel (Specific to WPF and Silverlight)
MVVM Model C# classes Might be a Gateway to data : WCF, REST, WebClient View Model C# Classes Might provide more properties like Visibility based on state (like admin) View XAML Code Behind.
Class Project XAML :Create a Square (rectangle with same size) Bind the height to width. XAML :Create a slider. Bind slider value to height. Add a Text : 1 –red, 2 –black, 3 –green Use IValueConverter: Convert, ConvertBack. <Rectangle background=“{Binding ElementName=“text”, Path=“Text”, Converter={StaticResource myConverter}> <User.Resources> <local:NumToBackgroundConverter x:Key=“myConverter” /> Public class NumToBackgroundConverter : IValueConverter Convert method : parse the int and retuen Brushes.Red, Green etc.

Más contenido relacionado

Destacado

Silverlight week2
Silverlight week2Silverlight week2
Silverlight week2iedotnetug
 
IEDOTNETUG Silverlight Class Week 6
IEDOTNETUG Silverlight Class Week 6IEDOTNETUG Silverlight Class Week 6
IEDOTNETUG Silverlight Class Week 6iedotnetug
 
Ria services updating data
Ria services updating dataRia services updating data
Ria services updating dataiedotnetug
 
Inland Empire .NET User's Group Silverlight Class
Inland Empire .NET User's Group Silverlight ClassInland Empire .NET User's Group Silverlight Class
Inland Empire .NET User's Group Silverlight Classiedotnetug
 
Fiziskās kultūras un sporta loma un vieta latvijā final
Fiziskās kultūras un sporta loma un vieta latvijā finalFiziskās kultūras un sporta loma un vieta latvijā final
Fiziskās kultūras un sporta loma un vieta latvijā finalAivArs Platonovs
 
Study: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsStudy: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsLinkedIn
 
Hype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerHype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerLuminary Labs
 

Destacado (8)

Silverlight week2
Silverlight week2Silverlight week2
Silverlight week2
 
IEDOTNETUG Silverlight Class Week 6
IEDOTNETUG Silverlight Class Week 6IEDOTNETUG Silverlight Class Week 6
IEDOTNETUG Silverlight Class Week 6
 
Ria services updating data
Ria services updating dataRia services updating data
Ria services updating data
 
Inland Empire .NET User's Group Silverlight Class
Inland Empire .NET User's Group Silverlight ClassInland Empire .NET User's Group Silverlight Class
Inland Empire .NET User's Group Silverlight Class
 
Gamification
GamificationGamification
Gamification
 
Fiziskās kultūras un sporta loma un vieta latvijā final
Fiziskās kultūras un sporta loma un vieta latvijā finalFiziskās kultūras un sporta loma un vieta latvijā final
Fiziskās kultūras un sporta loma un vieta latvijā final
 
Study: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsStudy: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving Cars
 
Hype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerHype vs. Reality: The AI Explainer
Hype vs. Reality: The AI Explainer
 

Similar a Silverlight week5

ADO.NET Entity Framework by Jose A. Blakeley and Michael Pizzo
ADO.NET Entity Framework by Jose A. Blakeley and Michael PizzoADO.NET Entity Framework by Jose A. Blakeley and Michael Pizzo
ADO.NET Entity Framework by Jose A. Blakeley and Michael PizzoHasnain Iqbal
 
Flex3 data binding
Flex3 data bindingFlex3 data binding
Flex3 data bindingguestdf3003
 
Entity Framework v2 Best Practices
Entity Framework v2 Best PracticesEntity Framework v2 Best Practices
Entity Framework v2 Best PracticesAndri Yadi
 
Web 10mca556 syllabs
Web 10mca556 syllabsWeb 10mca556 syllabs
Web 10mca556 syllabsRamesh Bindu
 
PATTERNS07 - Data Representation in C#
PATTERNS07 - Data Representation in C#PATTERNS07 - Data Representation in C#
PATTERNS07 - Data Representation in C#Michael Heron
 
Flex3 Deep Dive Final
Flex3 Deep Dive FinalFlex3 Deep Dive Final
Flex3 Deep Dive FinalRJ Owen
 
Silverlight 5 whats new overview
Silverlight 5 whats new overviewSilverlight 5 whats new overview
Silverlight 5 whats new overviewmdc11
 
ChircuVictor StefircaMadalin rad_aspmvc3_wcf_vs2010
ChircuVictor StefircaMadalin rad_aspmvc3_wcf_vs2010ChircuVictor StefircaMadalin rad_aspmvc3_wcf_vs2010
ChircuVictor StefircaMadalin rad_aspmvc3_wcf_vs2010vchircu
 
Ado.Net Data Services (Astoria)
Ado.Net Data Services (Astoria)Ado.Net Data Services (Astoria)
Ado.Net Data Services (Astoria)Igor Moochnick
 
Silverlight Developer Introduction
Silverlight   Developer IntroductionSilverlight   Developer Introduction
Silverlight Developer IntroductionTomy Ismail
 
Intake 38 data access 5
Intake 38 data access 5Intake 38 data access 5
Intake 38 data access 5Mahmoud Ouf
 
Chapter 3: ado.net
Chapter 3: ado.netChapter 3: ado.net
Chapter 3: ado.netNgeam Soly
 
3-ADO.NET.pdf
3-ADO.NET.pdf3-ADO.NET.pdf
3-ADO.NET.pdfManalAg
 

Similar a Silverlight week5 (20)

Simple Data Binding
Simple Data BindingSimple Data Binding
Simple Data Binding
 
Silverlight Databinding
Silverlight DatabindingSilverlight Databinding
Silverlight Databinding
 
WPF and Databases
WPF and DatabasesWPF and Databases
WPF and Databases
 
ADO.NET Entity Framework by Jose A. Blakeley and Michael Pizzo
ADO.NET Entity Framework by Jose A. Blakeley and Michael PizzoADO.NET Entity Framework by Jose A. Blakeley and Michael Pizzo
ADO.NET Entity Framework by Jose A. Blakeley and Michael Pizzo
 
Flex3 data binding
Flex3 data bindingFlex3 data binding
Flex3 data binding
 
ASP.NET Lecture 4
ASP.NET Lecture 4ASP.NET Lecture 4
ASP.NET Lecture 4
 
Entity Framework v2 Best Practices
Entity Framework v2 Best PracticesEntity Framework v2 Best Practices
Entity Framework v2 Best Practices
 
Web 10mca556 syllabs
Web 10mca556 syllabsWeb 10mca556 syllabs
Web 10mca556 syllabs
 
Dev308
Dev308Dev308
Dev308
 
PATTERNS07 - Data Representation in C#
PATTERNS07 - Data Representation in C#PATTERNS07 - Data Representation in C#
PATTERNS07 - Data Representation in C#
 
Flex3 Deep Dive Final
Flex3 Deep Dive FinalFlex3 Deep Dive Final
Flex3 Deep Dive Final
 
Silverlight 5 whats new overview
Silverlight 5 whats new overviewSilverlight 5 whats new overview
Silverlight 5 whats new overview
 
ChircuVictor StefircaMadalin rad_aspmvc3_wcf_vs2010
ChircuVictor StefircaMadalin rad_aspmvc3_wcf_vs2010ChircuVictor StefircaMadalin rad_aspmvc3_wcf_vs2010
ChircuVictor StefircaMadalin rad_aspmvc3_wcf_vs2010
 
Ado.Net Data Services (Astoria)
Ado.Net Data Services (Astoria)Ado.Net Data Services (Astoria)
Ado.Net Data Services (Astoria)
 
Silverlight Developer Introduction
Silverlight   Developer IntroductionSilverlight   Developer Introduction
Silverlight Developer Introduction
 
Lesson 05 Data Binding in WPF
Lesson 05 Data Binding in WPFLesson 05 Data Binding in WPF
Lesson 05 Data Binding in WPF
 
Intake 37 ef2
Intake 37 ef2Intake 37 ef2
Intake 37 ef2
 
Intake 38 data access 5
Intake 38 data access 5Intake 38 data access 5
Intake 38 data access 5
 
Chapter 3: ado.net
Chapter 3: ado.netChapter 3: ado.net
Chapter 3: ado.net
 
3-ADO.NET.pdf
3-ADO.NET.pdf3-ADO.NET.pdf
3-ADO.NET.pdf
 

Silverlight week5

  • 1. Silverlight 4 Course Introduction to Silverlight Layout Input Handling Applications, Resources, Deployment Data Binding, View Model Out of Browser, File Access, Printing WCF RIA Services (4 Weeks )
  • 2. Silverlight Week 5 Agenda  Binding Data contexts Data Templates Binding to collections MVVM ClassProject
  • 6. What is Data ? Database Table. XML In Memory Object Collections Web Service
  • 7. Data in Silverlight Silverlight is client side technology. You do not connect directly to the database table. Using some sort of service you query and get the data. Data comes in as XML, JSON etc. You convert it into some object or collection of objects. Bind UI to the object property.
  • 9. Data Binding Data could be Database, In memory objects, XML. Binding UI to object. In Silverlight binding could be done from one element’s property to another element’s property. A communication channel opens between the 2. Binding binding = new Binding(); binding.Source = person; binding.Path = new PropertyPath(“Age”); txtblock.SetBinding(TextBlock.TextProperty, binding); What if several property of same source needs to be bound ?
  • 10. Binding in Code Behind FrameworkElement’s SetBinding() : Any UI element derived from FrameworkElement. UI element is the target. BindingOperations.SetBinding() : Static method to support DependencyObject. Target specified as first parameter.
  • 11. Binding in XAML Binding Expressions uses Markup Extensions. <TextBox Text=“{Binding Path=Surname}” /> Text is the target property whereas Surname is the source property. Which object’s surname property to use ? Determined by DataContext.
  • 12. Data Contexts All UIElements have a property called “DataContext”. Property Value Inheritance helps us to propagate the DataContext set on the Root Element. PVI is equivalent to Ambient Property in Windows Forms. Data Binding Mode specifies the communication mode. One way (Silverlight Default) 2 way (WPF Default) One Time ChangeNotification is the mechanism used by Source Property. INotifyChanged Interface.
  • 13. Demo Create a Person object with properties : Firstname (String) Lastname (String) Age (int) MainPage create a person Person src = new Person { FirstName = “John”, LastName = “Doe”, Age = 25 }; Set data context in constructor : this.DataContext = src XAML : <TextBox Text=“{Binding Path=FirstName}” /> Modify the age. No effect. Explain binding mode. Add a button to modify age. UI textbox is not updated. Why ? Explain INotify Later implement INotifyPropertyChanged interface.
  • 14. Clearing Binding BindingOperations.ClearBinding(txtblock, TextBlock.Textproperty) BindingOperations.ClearAllBinding(txtblock)
  • 15. Data Template How to present data in a UI. Used by ItemsControls like ListBox and ContentControls like Buttons. Data Templates are just XAML markup with Binding expression.
  • 16. Demo Data template Part1 Move the grid to <UserControl.Resources> <DataTemplate x:Key=“myTemplate”> Add ContentControl Binding to collection Part2 Change to List of persons Use a ListBox.
  • 17. Collections Binding INotifyCollectionChanged ObservableCollection<T> Demo : Add a button and add new item to collection. UI not updated. Why ? br />Change List to ObservableCollection.
  • 18. MVVM What is it trying to solve ? Often Code behind has validation logic, interaction logic, application logic. Tightly coupled which causes issues: Less maintainability Less manageability Testing issues Ex : ComboBox selected item.
  • 19. Various Models MVC : Model View Controller MVP : Model View Presenter Presentation Model : Martin Fowler MVVM : Model View ViewModel (Specific to WPF and Silverlight)
  • 20. MVVM Model C# classes Might be a Gateway to data : WCF, REST, WebClient View Model C# Classes Might provide more properties like Visibility based on state (like admin) View XAML Code Behind.
  • 21. Class Project XAML :Create a Square (rectangle with same size) Bind the height to width. XAML :Create a slider. Bind slider value to height. Add a Text : 1 –red, 2 –black, 3 –green Use IValueConverter: Convert, ConvertBack. <Rectangle background=“{Binding ElementName=“text”, Path=“Text”, Converter={StaticResource myConverter}> <User.Resources> <local:NumToBackgroundConverter x:Key=“myConverter” /> Public class NumToBackgroundConverter : IValueConverter Convert method : parse the int and retuen Brushes.Red, Green etc.