binding lists in wpf
DESCRIPTION
academy.telerik.com/.../ desktop-applications- csharp - wpf. Binding Lists in WPF. Doncho Minkov. Telerik Software Academy. academy.telerik.com. Technical Trainer. http://www.minkov.it. Table of Contents. Complex Binding in WPF Accessing the " SelectedItem " - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/1.jpg)
Doncho Minkov
Telerik Software Academyacademy.telerik.com
Technical Trainerhttp://www.minkov.it
Binding Lists in WPF
academy.telerik.com/.../desktop-applications-csharp-wpf
![Page 2: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/2.jpg)
Table of Contents1. Complex Binding in WPF
Accessing the "SelectedItem" Using DisplayMemberPath and ValueMemberPath
2. Using Look-up Bindings3. Using Data Templates4. Sorting, Filtering and Grouping
Items from a Collection View
2
![Page 3: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/3.jpg)
Table of Contents (2)7. Data Source Providers
Object Relational XML
8. Master-detail Binding9. Hierarchical Binding
3
![Page 4: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/4.jpg)
Complex Data BindingBinding to a Collection of Items
![Page 5: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/5.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/6.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/7.jpg)
Complex Data BindingLive Demo
![Page 8: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/8.jpg)
Accessing the "Current
Item"
![Page 9: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/9.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/10.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/11.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/12.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/13.jpg)
Navigating Between ItemsLive Demo
![Page 14: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/14.jpg)
Binding List ControlsDisplayMemberPath and
SelectedValuePath
![Page 15: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/15.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/16.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/17.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/18.jpg)
DisplayMemberPath and
SelectedValuePathLive Demo
![Page 19: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/19.jpg)
Using Look-up Bindings
![Page 20: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/20.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/21.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/22.jpg)
Using Look-up BindingsLive Demo
![Page 23: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/23.jpg)
Using Data Templates
![Page 24: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/24.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/25.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/26.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/27.jpg)
Sorting Items
![Page 28: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/28.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/29.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/30.jpg)
Sorting ItemsLive Demo
![Page 31: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/31.jpg)
Filtering
![Page 32: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/32.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/33.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/34.jpg)
Grouping
![Page 35: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/35.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/36.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/37.jpg)
Filtering and GroupingLive Demo
![Page 38: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/38.jpg)
Declarative Sorting and
Grouping
![Page 39: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/39.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/40.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/41.jpg)
Declarative Sorting and
GroupingLive Demo
![Page 42: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/42.jpg)
Data Source
Providers
![Page 43: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/43.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/44.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/45.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/46.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/47.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/48.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/49.jpg)
Binding to Relational DataLive Demo
![Page 50: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/50.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/51.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/52.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/53.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/54.jpg)
XML Data Source ProviderLive Demo
![Page 55: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/55.jpg)
Master-Detail Binding
![Page 56: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/56.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/57.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/58.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/59.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/60.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/61.jpg)
Master-Details BindingLive Demo
![Page 62: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/62.jpg)
Hierarchical Binding
![Page 63: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/63.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/64.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/65.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/66.jpg)
форум програмиране, форум уеб дизайнкурсове и уроци по програмиране, уеб дизайн – безплатно
програмиране за деца – безплатни курсове и уроцибезплатен 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, PhoneGapfree C# book, безплатна книга C#, книга Java, книга C# Дончо Минков - сайт за програмиране
Николай Костов - блог за програмиранеC# курс, програмиране, безплатно
?? ? ?
??? ?
?
? ?
??
?
?
? ?
Questions?
?http://academy.telerik.com
Binding Lists
![Page 67: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/67.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/68.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/69.jpg)
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: Binding Lists in WPF](https://reader033.vdocuments.us/reader033/viewer/2022061617/56816388550346895dd477b9/html5/thumbnails/70.jpg)
Free Trainings @ Telerik Academy
Desktop Applications with C# and WPF
academy.telerik.com/.../desktop-applications-csharp-wpf
Telerik Software Academy academy.telerik.com
Telerik Academy @ Facebook facebook.com/TelerikAcademy
Telerik Software Academy Forums forums.academy.telerik.com