Svetlin Nakov Telerik Corporation www.telerik.com.

Slides:



Advertisements
Similar presentations
Dinko Jakovljević Microsoft Student Partner | BambooLab
Advertisements

Unit 5: Building Presentation Layer Applications with ASP.NET 2.0.
Resource Discovery Module DigiTool Version 3.0. Resource Discovery 2 Deposit Approval Search & Index Dispatcher & Viewers Single & Bulk Web Services DigiTool.
Programming Based on Events
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
Information systems and databases Database information systems Read the textbook: Chapter 2: Information systems and databases FOR MORE INFO...
Linq-to-Entities, Data Persisters, Master Datail Doncho Minkov Telerik School Academy Technical Trainer
® IBM Software Group © 2006 IBM Corporation The Eclipse Data Perspective and Database Explorer This section describes how to use the Eclipse Data Perspective,
Working with SharePoint Document Libraries. What are document libraries? Document libraries are collections of files that you can share with team members.
From VS C# 2010 Programming, John Allwork 1 VS2010 C# Programming - DB intro 1 Topics – Database Relational - linked tables SQL ADO.NET objects Referencing.
Tutorial: Introduction to ASP.NET Internet Technologies and Web Application 4 th February 2010.
CHAPTER 9 DATABASE MANAGEMENT © Prepared By: Razif Razali.
Databases and LINQ Visual Basic 2010 How to Program 1.
Chapter 11 Introduction to Database Processing. Class 11: Database Processing Use a Visual Studio Wizard to establish a database connection used to load.
Programming with Visual Basic.NET An Object-Oriented Approach  Chapter 8 Introduction to Database Processing.
Advanced Forms Lesson 10.
Introduction to ADO.Net and Visual Studio Database Tools ISYS 512.
Doncho Minkov Telerik School Academy academy.telerik.com Technical Trainer
® IBM Software Group © 2009 IBM Corporation Rational Publishing Engine RQM Multi Level Report Tutorial David Rennie, IBM Rational Services A/NZ
CST238 Week 7 Questions / Concerns? Announcements – HW#2 due today (project concept/preliminary design) – Check-off Take Home lab#6 Friday is the last.
XP New Perspectives on XML, 2 nd Edition Tutorial 10 1 WORKING WITH THE DOCUMENT OBJECT MODEL TUTORIAL 10.
Target: dependency property Source: any public property CLR and WPF properties Target: dependency property Source: any public property CLR and WPF properties.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
Last lecture data binding, binding object markup extension bind to any prop with INotifyPropertyChanged, target must be dep. prop. bind to obj... so no.
Windows Presentation Foundation. Agenda Introduction Developing Applications WPF and WF interoperability Custom Controls Styles and Templates Data Binding.
Windows Presentation Foundation Adam Calderon Principal Engineer Interknowlogy LLC
Open Data Protocol * Han Wang 11/30/2012 *
Adam Calderon – C# MVP Application Development Practice Lead Interknowlogy.
Ventsislav Popov Crossroad Ltd.. 1. ASP.NET Data Source Controls  SqlDataSource  EntityDataSource  ObjectDataSource 2. Entity Data Model and ADO.NET.
Basic WPF Controls Doncho Minkov Telerik School Academy schoolacademy.telerik.com Technical Trainer
V 1.0 Programming III. Automatic notifications (…Changed, INofityPropertyChanged, ObservableCollection ) Data formatters Data conversions Resources.
LAYOUT CONTROLS. XAML Overview XAML : eXtensible Application Markup Language pronounced (ZAMEL) is a markup language used to design user interfaces XML-based.
Binding UI Components to Data. Adding UI Components to the Page You can create components on a page by: Dragging a component from the Component Palette.
Meeting #6 – April 2011 – Web-Technologies Homework Assignments Svetlin Nakov Telerik Corporation
Working with GridView Control: Adding Columns. Adding Buttons to a Bound GridView: 1. Drag the WebProduct table from Data connection to a page 2. Demo.
CSCI 6962: Server-side Design and Programming Database Manipulation in ASP.
WaveMaker Visual AJAX Studio 4.0 Training Basics: Building Your First Application Binding Basics.
1 Introduction  Extensible Markup Language (XML) –Uses tags to describe the structure of a document –Simplifies the process of sharing information –Extensible.
Using Adapter Wizard ISYS 512. Data Adapter Wizard – 2 nd Level of Using ADO.Net Configure Data Adapter and generating a dataset: –From the Data tab of.
Access  Getting Started  Creating Tables  Designing Tables Worksheet #8.
CSCI 3327 Visual Basic Chapter 13: Databases and LINQ UTPA – Fall 2011.
Module 7 Data Binding to Collections. Module Overview Binding to Collections of Objects Using Collection Views Create Master-Detail User Interfaces Using.
Windows Presentation Foundation (WPF). Introduction Separates appearance of user interface from behavior Appearance usually specified by XAML Behavior.
Introduction to ADO.Net and VS Database Tools and Data Binding ISYS 350.
Ventsislav Popov Crossroad Ltd.. 1. ASP.NET Data Source Controls  SqlDataSource  EntityDataSource  ObjectDataSource 2. Entity Data Model and ADO.NET.
Module 14 Application Settings, State, and Life Cycle.
ControlTemplate and DataTemplate Doncho Minkov Telerik School Academy Technical Trainer
Module 3 Designing and Developing a User Interface.
.NET Framework Presentation. About Me Patrik Löwendahl –C# MVP –Certified Vista Touchdown Trainer Cornerstone
V 1.0 Programming III. Automatic notifications with data binding (…Changed, INofityPropertyChanged, ObservableCollection, DataTemplate) Data formatters.
Unit 6 Repetition Processing Instructor: Brent Presley.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Address Book Application Introducing Database Programming.
Understand Databound Controls Windows Development Fundamentals LESSON 4.2A.
Introduction to ADO.Net and Visual Studio Database Tools ISYS 350.
Module 10: Data Access in ASP.NET. Overview Overview of the ASP.NET Data Access What is Data Access List of Data Source Control What is Data Bound How.
Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Presentation Foundation Ruwan Wijesinghe.
Part of the Microsoft.NET Framework 3.0 Tomer Shamam.NET Technologies Expert Sela Group
Data Binding, Binding Properties Doncho Minkov Telerik School Academy Technical Trainer
Adam Schultz MVVM and WPF. MVVM Model, View, ViewModel A software architecture designed to separate out User Interface design, Business Logic, and Data.
Product Training Program
In Windows 8 Store Applications
Visual Basic 2010 How to Program
Avalon – Using Data In Your Applications
XAML User Interface Creation in C#
An introduction to MVVM using WPF NISCHAL S
Lists and List Items in Windows Runtime Apps
Lecture Set 10 Windows Controls and Forms
DEV312 基于WPF的数据绑定.
Introduction to ADO.Net and Visual Studio Database Tools.
Presentation transcript:

Svetlin Nakov Telerik Corporation

1. Complex Binding in WPF  Accessing the " SelectedItem "  Using DisplayMemberPath and ValueMemberPath 2. Using Look-up Bindings 3. Using Data Templates 4. Sorting, Filtering and Grouping Items from a Collection View 2

7. Data Source Providers  Object  Relational  XML 8. Master-detail Binding 9. Hierarchical Binding 3

Binding to a Collection of Items

 Binding to a list data source is exactly the same way as if we were binding to a single object data source 5 // Create an alias for a generic type so that we // can create a list of Person objects in XAML class People : List { } </local:People>

 Each TextBox can be bound to a property from only a single Person object  In this example the TextBox will be bound to the first item in the collection ( i.e. "Tom") 6 … Name: Name: <TextBox Text="{Binding Path=Age}" <TextBox Text="{Binding Path=Age}" Foreground="{Binding Path=Age, Converter=…}" Foreground="{Binding Path=Age, Converter=…}" … …/>

Live Demo

 The text box properties can be bound to only a single object at a time  The binding engine is giving them the current item in the list of objects 9

 Collection view in WPF  A mediator between the data bound control and the collection of items  Accessed through CollectionViewSource  The job of the collection view is to provide services on top of the data  Control of the current item  Sorting  Filtering  Grouping 10

 Getting the current item of bound collection: 11 public partial class MainWindow : Window { … private void birthdayButton_Click(object sender, private void birthdayButton_Click(object sender, RoutedEventArgs e) RoutedEventArgs e) { People people = (People)this.FindResource("Family"); People people = (People)this.FindResource("Family"); ICollectionView view = ICollectionView view = CollectionViewSource.GetDefaultView(people); CollectionViewSource.GetDefaultView(people); Person person = (Person)view.CurrentItem; Person person = (Person)view.CurrentItem; ++person.Age; ++person.Age; MessageBox.Show(person.Age.ToString()); MessageBox.Show(person.Age.ToString()); }}

 We can change which item is current  Using the MoveCurrentTo(…) methods of the ICollectionView interface 12 ICollectionView GetFamilyView() { People people =(People)this.FindResource("Family"); People people =(People)this.FindResource("Family"); return CollectionViewSource.GetDefaultView(people); return CollectionViewSource.GetDefaultView(people);} private void buttonBack_Click(object sender, RoutedEventArgs e) RoutedEventArgs e){ ICollectionView view = GetFamilyView(); ICollectionView view = GetFamilyView(); view.MoveCurrentToPrevious(); view.MoveCurrentToPrevious(); if (view.IsCurrentBeforeFirst) if (view.IsCurrentBeforeFirst) view.MoveCurrentToFirst(); view.MoveCurrentToFirst();}

Live Demo

DisplayMemberPath and SelectedValuePath

 List controls like ListBox and ComboBox display multiple items at a time  Can be bound to a collection in the DataContext  Can keep track of the current item  When binding the DisplayMemberPath specifies the property to be displayed  The SelectedValuePath specifies the property to be used as selected value (some ID) 15

 If we want to show every object of the Person class and display one of its properties  The ListBox class provides the DisplayMemberPath property 16 <ListBox ItemsSource="{Binding}" DisplayMemberPath="Name" DisplayMemberPath="Name" IsSynchronizedWithCurrentItem="True" IsSynchronizedWithCurrentItem="True"/>

 The ItemsControl class provides a path to describe the selected value of a piece of data  Data which is often used when the selection changes or an item is double-clicked 17 <ListBox Name="ListBoxPeople" ItemsSource="{Binding}" DisplayMemberPath="Name" SelectedValuePath="Age" /> DisplayMemberPath="Name" SelectedValuePath="Age" /> private void ListBoxPeople_SelectionChanged( object sender, SelectionChangedEventArgs e) object sender, SelectionChangedEventArgs e){ int index = ListBoxPerson.SelectedIndex; int index = ListBoxPerson.SelectedIndex; if (index < 0) { return; } if (index < 0) { return; } Person item = (Person) ListBoxPerson.SelectedItem; Person item = (Person) ListBoxPerson.SelectedItem; int value = (int) ListBoxPerson.SelectedValue; … int value = (int) ListBoxPerson.SelectedValue; …}

Live Demo

 We want to provide a UI that maps numbers to their textual representation in English  We must construct a NamedAge type for use in populating a look-up table 20 public class NamedAge { public string NameForAge { get; set; } public string NameForAge { get; set; } public int AgeId { get; set; } public int AgeId { get; set; }} class NamedAges : List { }

 Populate the table for looking up  The final step is the bit of binding that tells the ComboBox control where to get the currently selected value 21 </local:NamedAges> <ComboBox Name="ComboBoxNumbers" ItemsSource= "{Binding Source={StaticResource NamedAgeLookup}}" "{Binding Source={StaticResource NamedAgeLookup}}" DisplayMemberPath="NameForAge" DisplayMemberPath="NameForAge" SelectedValuePath="AgeId" SelectedValuePath="AgeId" SelectedValue="{Binding Path=Age}" /> SelectedValue="{Binding Path=Age}" />

Live Demo

 Data templates allow displaying more than one property from a custom class  A data template is a tree of elements to expand in a particular context  For example, for each Person object, you might like to be able to concatenate the name and age together  This is a logical template that looks like this  Name (age:Age) 24

 To define this template for items in the ListBox, we create a DataTemplate element 25 (age: (age: <TextBlock Text="{Binding Path=Age}" <TextBlock Text="{Binding Path=Age}" Foreground="{Binding Path=Age, Foreground="{Binding Path=Age, Converter={StaticResource ageConverter}}" />) Converter={StaticResource ageConverter}}" />) </ListBox>

 The ListBox control has an ItemTemplate property  Accepts an instance of the DataTemplate class  The ListBox shows all the items in the collection 26

 The view allows us to do a number of things to the data before it’s displayed  Including changing the order in which the data is shown  The simplest way to sort is by manipulating the SortDescriptions property of the view  Also we can provide the view with a custom sorting by implementing IComparer 28

 Sorting items view in WPF: 29 private void buttonSort_Click (object sender, RoutedEventArgs e) (object sender, RoutedEventArgs e){ ICollectionView view = GetFamilyView(); ICollectionView view = GetFamilyView(); if (view.SortDescriptions.Count == 0) if (view.SortDescriptions.Count == 0) { { view.SortDescriptions.Add( view.SortDescriptions.Add( new SortDescription("Name", new SortDescription("Name", ListSortDirection.Ascending)); ListSortDirection.Ascending)); view.SortDescriptions.Add( view.SortDescriptions.Add( new SortDescription("Age", new SortDescription("Age", ListSortDirection.Descending)); ListSortDirection.Descending)); } else else view.SortDescriptions.Clear(); view.SortDescriptions.Clear();}

Live Demo

 If we want to filter the objects from the view by some criteria  We need to feed the view an implementation of the Predicate delegate  Takes a single object parameter and returns a Boolean 32 private void buttonFilter_Click(object sender, RoutedEventArgs e) RoutedEventArgs e){ ICollectionView view = GetFamilyView(); ICollectionView view = GetFamilyView(); // the example continues

33 if (view.Filter == null) { view.Filter = delegate(object item) view.Filter = delegate(object item) { return ((Person)item).Age >= 25; return ((Person)item).Age >= 25; }; };} else { view.Filter = null; } } // The result is: // The result is:

 To set up grouping  Establish the groups you would like to use  Manipulating the GroupDescriptions collection on your view 35 if (view.GroupDescriptions.Count == 0) { view.GroupDescriptions.Add( view.GroupDescriptions.Add( new PropertyGroupDescription("Age")); new PropertyGroupDescription("Age"));}else{ view.GroupDescriptions.Clear(); view.GroupDescriptions.Clear();}

 The PropertyGroupDescription object  Takes the name of the property you would like to use for grouping  GroupStyle  Collection of group visualization related information 36 </ListBox>

Live Demo

 Bring in the System.ComponentModel and System.Windows.Data namespaces  Create SortDescription and PropertyGroupDescription objects  Then create a CollectionViewSource object, which sorts and groups the data  Exposes an ICollectionView implementation 39 xmlns:compModel="clr-namespace:System.ComponentModel; assembly=WindowsBase" xmlns:data="clr-namespace:System.Windows.Data;assembly=PresentationFramework">

40 <CollectionViewSource x:Key="SortedGroupedFamily" Source="{StaticResource Family}"> Source="{StaticResource Family}"> <compModel:SortDescription PropertyName="Name" <compModel:SortDescription PropertyName="Name" Direction="Ascending" /> Direction="Ascending" /> <compModel:SortDescription PropertyName="Age" <compModel:SortDescription PropertyName="Age" Direction="Descending" /> Direction="Descending" /> <data:PropertyGroupDescription <data:PropertyGroupDescription PropertyName="Age" PropertyName="Age" Converter="{StaticResource ageConverter}" /> Converter="{StaticResource ageConverter}" /> <data:PropertyGroupDescription <data:PropertyGroupDescription PropertyName="Age" /> PropertyName="Age" />

Live Demo

 Data Providers are wrappers around existing data models (relational data, XML, …)  Used to simplify data binding with DB or XML  WPF works with two data source providers  ObjectDataProvider  XmlDataProvider  Both derive from DataSourceProvider  Data source providers create a layer of indirection for any kind of operation 43

 Load a set of Person from some source  LoadPeople method will load people however it also returns that data for binding 44 public class Person : INotifyPropertyChanged { … } public class People : ObservableCollection {} public class RemotePeopleLoader { public People LoadPeople() public People LoadPeople() { // Load people from somewhere // Load people from somewhere People people = new People( ); … People people = new People( ); … return people; return people; } … } …}

 Create the RemotePeopleLoader and call the LoadPeople method in XAML file  ObjectType specifies the type of the class to create  The MethodName specifies the name of the method to call to retrieve the data <ObjectDataProvider x:Key="Family" <ObjectDataProvider x:Key="Family" ObjectType="{x:Type local:RemotePeopleLoader}" ObjectType="{x:Type local:RemotePeopleLoader}" MethodName="LoadPeople" /> MethodName="LoadPeople" /></Window.Resources>

 We create a database with one table " People "  Using Solution Explorer add LINQ-to-Entities mappings  Drag the People table from the Database Explorer  Add an instance of DataClassesPeopleDataContext in.xaml.cs 46 DataClassesPeopleDataContext dataContextPeople = new DataClassesPeopleDataContext(); new DataClassesPeopleDataContext();

 Binding to relational data declaratively 47 <Window.Resources> </Window.Resources> <ListBox Name="ListBoxPeople" ItemTemplate= "{StaticResource DataTemplatePersonName }"/> "{StaticResource DataTemplatePersonName }"/>

 Adding new records to the database  Committing the changes to database 48 People newPerson = new People(); newPerson.PersonName = TextBoxAdd.Text; dataContexPeople.Peoples.InsertOnSubmit(newPerson); dataContexPeople.SubmitChanges();

Live Demo

 WPF also supports binding to XML data  We can bind to it using the XmlDataProvider 50 <Window.Resources> <XmlDataProvider <XmlDataProvider x:Key="Family" x:Key="Family" Source="family.xml" Source="family.xml" XPath="/sb:Family/sb:Person"> XPath="/sb:Family/sb:Person"> <XmlNamespaceMapping Prefix="sb" <XmlNamespaceMapping Prefix="sb" Uri=" /> Uri=" />

 Use of the XmlDataProvider with a relative URL that points to the family.xml  Using namespace prefixes in the XAML makes it possible to construct the XPath statement 51 … <TextBlock Text="{Binding <TextBlock Text="{Binding Foreground="{Binding Foreground="{Binding Converter= {StaticResource ageConverter}}" /> Converter= {StaticResource ageConverter}}" /> </StackPanel>…

 In the XML data binding we use XmlDocument and XmlElement  For updating and accessing values, use the XmlElement.SetAttribute method  Sorting or grouping is also supported, but paths are preceded ) 52 void birthdayButton_Click(object sender, RoutedEventArgs e) RoutedEventArgs e){ ICollectionView view = GetFamilyView( ); ICollectionView view = GetFamilyView( ); XmlElement person = (XmlElement)view.CurrentItem; XmlElement person = (XmlElement)view.CurrentItem; // the example continues

53 person.SetAttribute("Age", (int.Parse( person.Attributes["Age"].Value) + 1).ToString( )); person.Attributes["Age"].Value) + 1).ToString( ));MessageBox.Show( string.Format("Happy Birthday, {0}, age {1}!", string.Format("Happy Birthday, {0}, age {1}!", person.Attributes["Name"].Value, person.Attributes["Name"].Value, person.Attributes["Age"].Value), person.Attributes["Age"].Value), "Birthday"); "Birthday");}… void groupButton_Click(object sender, RoutedEventArgs e) { ICollectionView view = GetFamilyView( ); ICollectionView view = GetFamilyView( ); if( view.GroupDescriptions.Count == 0 ) if( view.GroupDescriptions.Count == 0 ) { view.GroupDescriptions.Add(new view.GroupDescriptions.Add(new } else { view.GroupDescriptions.Clear(); } else { view.GroupDescriptions.Clear(); }}

Live Demo

 Master-details binding means to bind two related lists  Selecting a row in the first list shows its detail rows in the second list  You need to have a parent data object that provides a collection of related child objects  Master-details binding is a form of filtering  Where the selection in the master list acts as filtering parameter for the associated detail list 56

 In previous example we have families and people  Instances of Families, Family, People, and Person looked like this 57  The Families collection is the master data  It is holding instances of the Family class  Each of which holds Members property of type People  Which holds the detail Person

 Declaring master-detail data: 58 <Window.Resources> … … </Window.Resources>

 Binding to master Family data: 59 <Window.Resources> … … </Window.Resources> … <TextBlock Grid.Row="0" <TextBlock Grid.Row="0" Grid.Column="0">Families: Grid.Column="0">Families: <ListBox Grid.Row="1" Grid.Column="0" <ListBox Grid.Row="1" Grid.Column="0" IsSynchronizedWithCurrentItem="True" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}"> ItemsSource="{Binding}">

 Binding to detail Person data: … … <StackPanel Grid.Row="0" Grid.Column="1" <StackPanel Grid.Row="0" Grid.Column="1" Orientation="Horizontal"> Orientation="Horizontal"> <ListBox Grid.Row="1" Grid.Column="1" <ListBox Grid.Row="1" Grid.Column="1" IsSynchronizedWithCurrentItem="True" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Path=Members}" > ItemsSource="{Binding Path=Members}" > … …

Live Demo

 Hierarchical binding generally involves some number of levels, unknown until runtime  E.g. a tree of items, each with few child items  Control that can expand itself as appropriate, like a menu or a tree needs hierarchical binding  WPF has built-in support for hierarchical binding using a special kind of data template  Knows both how to display the current level of data and where to go for the next level 63

 Binding a TreeView control’s root item  Provide a data template 64 </Window.Resources>… <TreeViewItem ItemsSource="{Binding}" <TreeViewItem ItemsSource="{Binding}" Header="Families" /> Header="Families" />

 HierarchicalDataTemplate element  Provides the ItemsSource property so that the tree can keep digging into the data 65 <HierarchicalDataTemplate DataType="{x:Type local:Family}" ItemsSource="{Binding Path=Members}"> ItemsSource="{Binding Path=Members}"> </HierarchicalDataTemplate> <HierarchicalDataTemplate DataType="{x:Type local:Person}" ItemsSource="{Binding Path=Traits}"> ItemsSource="{Binding Path=Traits}"> (age: ) (age: ) </HierarchicalDataTemplate>

Questions?

1. Write a program to manage a simple system with information about towns and countries. Each country is described by name, language, national flag and list of towns. Each town is described by name, population and country. You should create navigation over the towns and countries. Enable editing the information about them. Don't use list controls but only text boxes and simple binding 2. Rewrite the previous exercise using list controls. 67

3. Create a database with two tables – Categories and Products. Each category has category name. Each product has category, model number, model name unit cost, and description. Consider the simple window look like the screenshot below: 68 Design a form to view products by ID and bind all controls to their relevant columns from the database tables.

4. Using complex data binding create a system, resembling the system from the first exercise (towns and countries). Add to the system a set of continents – each country is in one of them. Display data and enable navigation. Load and save the data in a XML file. Add sorting, filtering and grouping functions. Use master-details bindings. 5. Rewrite the previous exercise to use database and LINQ-to-SQL. Ensure all entities can be added / edited / deleted (continents, countries and towns). 69