Transcript
Page 1: 7. XAML & WPF - Binding Lists

Doncho Minkov

Telerik Software Academyacademy.telerik.com

Technical Trainerhttp://www.minkov.it

Binding Lists in WPF

http://academy.telerik.com/

Page 2: 7. XAML & WPF - Binding Lists

Table of Contents1. 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

Page 3: 7. XAML & WPF - Binding Lists

Table of Contents (2)7. Data Source Providers

Object Relational XML

8. Master-detail Binding

9. Hierarchical Binding

3

Page 4: 7. XAML & WPF - Binding Lists

Complex Data BindingBinding to a Collection of Items

Page 5: 7. XAML & WPF - Binding Lists

Complex Binding

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 XAMLclass People : List<Person> { }<!-- Declaring a collection in XAML --> <local:People x:Key="Family"> <local:Person Name="Tom" Age="11" /> <local:Person Name="John" Age="12" /> <local:Person Name="Melissa" Age="38" /></local:People>

Page 6: 7. XAML & WPF - Binding Lists

Complex Binding (2)

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

<Grid DataContext="{StaticResource Family}"> … <TextBlock …>Name:</TextBlock> <TextBox Text="{Binding Path=Name}" … /> <TextBox Text="{Binding Path=Age}" Foreground="{Binding Path=Age, Converter=…}" …/>

Page 7: 7. XAML & WPF - Binding Lists

Complex Data Binding

Live Demo

Page 8: 7. XAML & WPF - Binding Lists

Accessing the "Current

Item"

Page 9: 7. XAML & WPF - Binding Lists

Accessing the "Current Item"

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

Page 10: 7. XAML & WPF - Binding Lists

Accessing the "Current Item" (3)

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

Page 11: 7. XAML & WPF - Binding Lists

Accessing the "Current Item" (2)

Getting the current item of bound collection:

11

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

Page 12: 7. XAML & WPF - Binding Lists

Navigating Between Items

We can change which item is current Using the MoveCurrentTo(…) methods

of the ICollectionView interface

12

ICollectionView GetFamilyView(){ People people =(People)this.FindResource("Family"); return CollectionViewSource.GetDefaultView(people);}private void buttonBack_Click(object sender, RoutedEventArgs e){ ICollectionView view = GetFamilyView(); view.MoveCurrentToPrevious(); if (view.IsCurrentBeforeFirst) view.MoveCurrentToFirst();}

Page 13: 7. XAML & WPF - Binding Lists

Navigating Between ItemsLive Demo

Page 14: 7. XAML & WPF - Binding Lists

Binding List ControlsDisplayMemberPath and

SelectedValuePath

Page 15: 7. XAML & WPF - Binding Lists

Binding List Controls 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

Page 16: 7. XAML & WPF - Binding Lists

DisplayMemberPath 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" IsSynchronizedWithCurrentItem="True"/> <!--The result is-->

Page 17: 7. XAML & WPF - Binding Lists

SelectedValuePath 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" />

private void ListBoxPeople_SelectionChanged( object sender, SelectionChangedEventArgs e){ int index = ListBoxPerson.SelectedIndex; if (index < 0) { return; } Person item = (Person) ListBoxPerson.SelectedItem; int value = (int) ListBoxPerson.SelectedValue; …}

Page 18: 7. XAML & WPF - Binding Lists

DisplayMemberPath and

SelectedValuePathLive Demo

Page 19: 7. XAML & WPF - Binding Lists

Using Look-up Bindings

Page 20: 7. XAML & WPF - Binding Lists

Using Look-up Bindings 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 int AgeId { get; set; }}

class NamedAges : List<NamedAge> { }

Page 21: 7. XAML & WPF - Binding Lists

Using Look-up Bindings (2)

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 x:Key="NamedAgeLookup"> <local:NamedAge NameForAge="zero" AgeId="0" /> <local:NamedAge NameForAge="one" AgeId="1" /></local:NamedAges>

<ComboBox Name="ComboBoxNumbers" ItemsSource= "{Binding Source={StaticResource NamedAgeLookup}}" DisplayMemberPath="NameForAge" SelectedValuePath="AgeId" SelectedValue="{Binding Path=Age}" />

Page 22: 7. XAML & WPF - Binding Lists

Using Look-up BindingsLive Demo

Page 23: 7. XAML & WPF - Binding Lists

Using Data Templates

Page 24: 7. XAML & WPF - Binding Lists

Using Data Templates

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

Page 25: 7. XAML & WPF - Binding Lists

Using Data Templates (2)

To define this template for items in the ListBox, we create a DataTemplate element

25

<ListBox ItemsSource="{Binding}"> <ListBox.ItemTemplate> <DataTemplate> <TextBlock> <TextBlock Text="{Binding Path=Name}" /> (age: <TextBlock Text="{Binding Path=Age}" Foreground="{Binding Path=Age, Converter={StaticResource ageConverter}}" />) </TextBlock> </DataTemplate> </ListBox.ItemTemplate></ListBox>

Page 26: 7. XAML & WPF - Binding Lists

Using Data Templates (2)

The ListBox control has an ItemTemplate property Accepts an instance of the DataTemplate class

The ListBox shows all the items in the collection

26

Page 27: 7. XAML & WPF - Binding Lists

Sorting Items

Page 28: 7. XAML & WPF - Binding Lists

Sorting Items 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

Page 29: 7. XAML & WPF - Binding Lists

Sorting Items (2) Sorting items view in WPF:

29

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

Page 30: 7. XAML & WPF - Binding Lists

Sorting ItemsLive Demo

Page 31: 7. XAML & WPF - Binding Lists

Filtering

Page 32: 7. XAML & WPF - Binding Lists

Filtering If we want to filter the objects from the view by some criteria

We need to feed the view an implementation of the Predicate<object> delegate Takes a single object parameter and

returns a Boolean

32

private void buttonFilter_Click(object sender, RoutedEventArgs e){ ICollectionView view = GetFamilyView();

// the example continues

Page 33: 7. XAML & WPF - Binding Lists

Filtering (2)

33

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

Page 34: 7. XAML & WPF - Binding Lists

Grouping

Page 35: 7. XAML & WPF - Binding Lists

Grouping

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( new PropertyGroupDescription("Age"));}else { view.GroupDescriptions.Clear();}

Page 36: 7. XAML & WPF - Binding Lists

Grouping (2) 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 … ItemsSource="{Binding}" > <ListBox.GroupStyle> <x:Static Member="GroupStyle.Default" /> </ListBox.GroupStyle></ListBox>

Page 37: 7. XAML & WPF - Binding Lists

Filtering and GroupingLive Demo

Page 38: 7. XAML & WPF - Binding Lists

Declarative Sorting and

Grouping

Page 39: 7. XAML & WPF - Binding Lists

Declarative Sorting and Grouping

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">

Page 40: 7. XAML & WPF - Binding Lists

Declarative Sorting and

Grouping (2)

40

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

Page 41: 7. XAML & WPF - Binding Lists

Declarative Sorting and

GroupingLive Demo

Page 42: 7. XAML & WPF - Binding Lists

Data Source

Providers

Page 43: 7. XAML & WPF - Binding Lists

Object Data Provider 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

Page 44: 7. XAML & WPF - Binding Lists

Object Data Provider – Example

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<Person> {}

public class RemotePeopleLoader { public People LoadPeople() { // Load people from somewhere People people = new People( ); … return people; } …}

Page 45: 7. XAML & WPF - Binding Lists

Object Data Provider – Example (2)

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

45

<Window.Resources> ... <ObjectDataProvider x:Key="Family" ObjectType="{x:Type local:RemotePeopleLoader}" MethodName="LoadPeople" /></Window.Resources>

Page 46: 7. XAML & WPF - Binding Lists

Binding to Relational Data

We create a database with one table "People"

Using Solution Explorer add LINQ-SQL mappings

Drag the People table from the Database Explorer

Add an instance of DataClassesPeopleDataContext in .xaml.cs

46

DataClassesPeopleDataContext dataContextPeople = new DataClassesPeopleDataContext();

Page 47: 7. XAML & WPF - Binding Lists

Binding to Relational Data (2)

Binding to relational data declaratively

47

<Window.Resources> <DataTemplate x:Key="DataTemplatePersonName"> <TextBlock Text="{Binding Path=PersonName}"/> </DataTemplate></Window.Resources> ...<ListBox Name="ListBoxPeople" ItemTemplate= "{StaticResource DataTemplatePersonName }"/>

Page 48: 7. XAML & WPF - Binding Lists

Binding to Relational Data (3)

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();

Page 49: 7. XAML & WPF - Binding Lists

Binding to Relational DataLive Demo

Page 50: 7. XAML & WPF - Binding Lists

XML Data Source Provider

WPF also supports binding to XML data

We can bind to it using the XmlDataProvider

50

<Window.Resources> <XmlDataProvider x:Key="Family" Source="family.xml" XPath="/sb:Family/sb:Person"> <XmlDataProvider.XmlNamespaceManager> <XmlNamespaceMappingCollection> <XmlNamespaceMapping Prefix="sb" Uri="http://sellsbrothers.com" /> </XmlNamespaceMappingCollection> </XmlDataProvider.XmlNamespaceManager> </XmlDataProvider> <!--the example continues-->

Page 51: 7. XAML & WPF - Binding Lists

XML Data Source Provider (2)

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

…<StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding XPath=@Name}" /> <TextBlock Text=" (age: " /> <TextBlock Text="{Binding XPath=@Age}" Foreground="{Binding XPath=@Age, Converter= {StaticResource ageConverter}}" /> <TextBlock Text=")" /></StackPanel>…

Page 52: 7. XAML & WPF - Binding Lists

XML Data Source Provider (3)

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 by @ (e.g., @Age)

52

void birthdayButton_Click(object sender, RoutedEventArgs e){ ICollectionView view = GetFamilyView( ); XmlElement person = (XmlElement)view.CurrentItem;

// the example continues

Page 53: 7. XAML & WPF - Binding Lists

XML Data Source Provider (4)

53

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

Page 54: 7. XAML & WPF - Binding Lists

XML Data Source ProviderLive Demo

Page 55: 7. XAML & WPF - Binding Lists

Master-Detail Binding

Page 56: 7. XAML & WPF - Binding Lists

Master-Details Binding 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

Page 57: 7. XAML & WPF - Binding Lists

Master-Details Binding (2)

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

Page 58: 7. XAML & WPF - Binding Lists

Master-Details Binding – Example

Declaring master-detail data:

58

<Window.Resources> <local:Families x:Key="Families"> <local:Family FamilyName="Piyandetata"> <local:Family.Members> <local:People> <local:Person Name="Joro Vodkata" Age="21" /> … </local:People> </local:Family.Members> </local:Family> <local:Family FamilyName="Addams"> <local:Family.Members> <local:People> <local:Person Name="Uncle Fester" Age="135" /> … </local:Families></Window.Resources>

Page 59: 7. XAML & WPF - Binding Lists

Master-Details Binding –

Example (2) Binding to master Family data:

59

<Window.Resources> <local:Families x:Key="Families">…</local:Families></Window.Resources><Grid DataContext="{StaticResource Families}"> … <!-- Families Column --> <TextBlock Grid.Row="0" Grid.Column="0">Families:</TextBlock> <ListBox Grid.Row="1" Grid.Column="0" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}"> <ListBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Path=FamilyName}" /> </DataTemplate> </ListBox.ItemTemplate> </ListBox>

Page 60: 7. XAML & WPF - Binding Lists

Master-Details Binding –

Example (3) Binding to detail Person data:

60

<Grid DataContext="{StaticResource Families}"> ... … <!-- Members Column --> <StackPanel Grid.Row="0" Grid.Column="1" Orientation="Horizontal"> <TextBlock Text="{Binding Path=FamilyName}" /> <TextBlock Text=" Family Members:" /> </StackPanel> <ListBox Grid.Row="1" Grid.Column="1" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Path=Members}" > <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Path=Name}" /> <TextBlock Text=" (age: " /> <TextBlock Text="{Binding Path=Age}" /> <TextBlock Text=" )" /> …

Page 61: 7. XAML & WPF - Binding Lists

Master-Details Binding

Live Demo

Page 62: 7. XAML & WPF - Binding Lists

Hierarchical Binding

Page 63: 7. XAML & WPF - Binding Lists

Hierarchical Binding 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

Page 64: 7. XAML & WPF - Binding Lists

Hierarchical Binding (2) Binding a TreeView control’s root item

Provide a data template

64

<DataTemplate DataType="{x:Type local:Family}"> <TextBlock Text="{Binding Path=FamilyName}" /> </DataTemplate></Window.Resources>… <TreeView DataContext="{StaticResource Families}"> <TreeViewItem ItemsSource="{Binding}" Header="Families" /> </TreeView>

Page 65: 7. XAML & WPF - Binding Lists

Hierarchical Binding (3) 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}"> <TextBlock Text="{Binding Path=FamilyName}" /></HierarchicalDataTemplate>

<HierarchicalDataTemplate DataType="{x:Type local:Person}" ItemsSource="{Binding Path=Traits}"> <TextBlock> <TextBlock Text="{Binding Path=Name}" /> (age: <TextBlock Text="{Binding Path=Age}" />) </TextBlock></HierarchicalDataTemplate>

Page 66: 7. XAML & WPF - Binding Lists

форум програмиране, форум уеб дизайнкурсове и уроци по програмиране, уеб дизайн – безплатно

програмиране за деца – безплатни курсове и уроцибезплатен SEO курс - оптимизация за търсачки

уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop

уроци по програмиране и уеб дизайн за ученициASP.NET MVC курс – HTML, SQL, C#, .NET, ASP.NET MVC

безплатен курс "Разработка на софтуер в cloud среда"

BG Coder - онлайн състезателна система - online judge

курсове и уроци по програмиране, книги – безплатно от Наков

безплатен курс "Качествен програмен код"

алго академия – състезателно програмиране, състезания

ASP.NET курс - уеб програмиране, бази данни, C#, .NET, ASP.NETкурсове и уроци по програмиране – Телерик академия

курс мобилни приложения с iPhone, Android, WP7, PhoneGap

free C# book, безплатна книга C#, книга Java, книга C#Дончо Минков - сайт за програмиранеНиколай Костов - блог за програмиранеC# курс, програмиране, безплатно

?

? ? ??

?? ?

?

?

?

??

?

?

? ?

Questions?

?http://academy.telerik.com

Binding Lists

Page 67: 7. XAML & WPF - Binding Lists

Exercises1. 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

Page 68: 7. XAML & WPF - Binding Lists

Exercises (2)

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.

Page 69: 7. XAML & WPF - Binding Lists

Exercises (3)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

Page 70: 7. XAML & WPF - Binding Lists

Free Trainings @ Telerik Academy

Desktop Applications with C# and WPF academy.telerik.com/

Telerik Software Academy academy.telerik.com

Telerik Academy @ Facebook facebook.com/TelerikAcademy

Telerik Software Academy Forums forums.academy.telerik.com


Recommended