19
Last lecture data binding, binding object markup extension bind to any prop with INotifyPropertyChanged, target must be dep. prop. bind to obj... so no prop. path binding to collection issynchronized with current item datacontext displaymemberpath, stringformat, datatemplate, valueconverter

Last lecture data binding, binding object markup extension bind to any prop with INotifyPropertyChanged, target must be dep. prop. bind to obj... so no

Embed Size (px)

Citation preview

Page 1: Last lecture data binding, binding object markup extension bind to any prop with INotifyPropertyChanged, target must be dep. prop. bind to obj... so no

Last lecture•data binding, binding object

•markup extension

•bind to any prop with INotifyPropertyChanged, target must be dep. prop.

•bind to obj... so no prop. path

•binding to collection

• issynchronized with current item

•datacontext

•displaymemberpath, stringformat, datatemplate, valueconverter

Page 2: Last lecture data binding, binding object markup extension bind to any prop with INotifyPropertyChanged, target must be dep. prop. bind to obj... so no

customizing collection view

•when isSynchronizedWithCurrentItem= true, where is the current item

•a default “view” inserted... obj impl ICollectionView... has also support for sorting, grouping, filtering, and navigating

•lets discuss these four and multiple views for the same source obj.

Page 3: Last lecture data binding, binding object markup extension bind to any prop with INotifyPropertyChanged, target must be dep. prop. bind to obj... so no

sorting

•SortDescriptions prop. is a coll. of SortDescription which chooses a field and order

•sort by datetime, then by name

•a clear method to return to unsorted

•eg. three buttons to sort and toggle

Page 4: Last lecture data binding, binding object markup extension bind to any prop with INotifyPropertyChanged, target must be dep. prop. bind to obj... so no
Page 5: Last lecture data binding, binding object markup extension bind to any prop with INotifyPropertyChanged, target must be dep. prop. bind to obj... so no

•no explicit relationship with ListBox

•if additional controls bound, they would sort with it

•Now a GroupDescriptions prop. containing PropertyGroupDescription obj.

•but it has no effect without the GroupStyle prop. set with a HeaderTemplate

Page 6: Last lecture data binding, binding object markup extension bind to any prop with INotifyPropertyChanged, target must be dep. prop. bind to obj... so no

•custom or builtin

•can use a valueconverter

•and pass to ctor

Page 7: Last lecture data binding, binding object markup extension bind to any prop with INotifyPropertyChanged, target must be dep. prop. bind to obj... so no
Page 8: Last lecture data binding, binding object markup extension bind to any prop with INotifyPropertyChanged, target must be dep. prop. bind to obj... so no

•sorting applied before grouping

•first sorting criteria should be same as grouping otherwise output doesnt make much sense

•can pass null to get total custom control in the value converter

Page 9: Last lecture data binding, binding object markup extension bind to any prop with INotifyPropertyChanged, target must be dep. prop. bind to obj... so no

filtering

•a property Filter of type Predicate<Object> its null by d

•e.g. show only photos from last 7 days

Page 10: Last lecture data binding, binding object markup extension bind to any prop with INotifyPropertyChanged, target must be dep. prop. bind to obj... so no

navigating•nav. means managing the current item, not

the other kind of nav.

• ICollectionView has CurrentItem CurrentPosition and also methods for changing them

•e.g. prev/next photo buttons are handled like this

•view initializes to 0 and first item

• listbox initializes to -1 and null (unselected)

Page 11: Last lecture data binding, binding object markup extension bind to any prop with INotifyPropertyChanged, target must be dep. prop. bind to obj... so no
Page 12: Last lecture data binding, binding object markup extension bind to any prop with INotifyPropertyChanged, target must be dep. prop. bind to obj... so no

prop. paths in Bindings

•useful for master/detail interfaces

•sorting, grouping, filtering automatic

•but nav. only when IsSynchronizedWithCurrentItem=true

•otherwise SelectedItem and CurrentItem are separate

Page 13: Last lecture data binding, binding object markup extension bind to any prop with INotifyPropertyChanged, target must be dep. prop. bind to obj... so no

additional views•CollectionViewSource can be used to create

new views and applied to targets

•binding paths

•remain same

Page 14: Last lecture data binding, binding object markup extension bind to any prop with INotifyPropertyChanged, target must be dep. prop. bind to obj... so no

•CollectionViewSource has its own SortDescriptions and GroupDescriptions properties and a Filter event to be used from xaml

•must include another namespace

Page 15: Last lecture data binding, binding object markup extension bind to any prop with INotifyPropertyChanged, target must be dep. prop. bind to obj... so no

•IsSynchronizedWithCurrentItem=true by d for custom views... have to explicitly set false... kind of a bit inconsistent... but we acknowledging view existance by custom view

Page 16: Last lecture data binding, binding object markup extension bind to any prop with INotifyPropertyChanged, target must be dep. prop. bind to obj... so no

Data Providers•source obj can be arbitrary... you could bind to db, registry,

excel spreadsheet etc. with enough code

•an obj. that exposes right props and notifications and handles messy details

•work involved might overweight benefits if writing all logic yourself

•two generic data-binding-friendly way to expose common items... XmlDataProvider and ObjectDataProvider

•starting with wpf 3.5 sp1 ... data binding works with LINQ (language independent query)... you can set Source or DataContext to a LINQ and the enumerable result used

•Now with LINQ to SQL and LINQ to XML, is an easy way than using wpf data providers

Page 17: Last lecture data binding, binding object markup extension bind to any prop with INotifyPropertyChanged, target must be dep. prop. bind to obj... so no

XmlDataProvider

Page 18: Last lecture data binding, binding object markup extension bind to any prop with INotifyPropertyChanged, target must be dep. prop. bind to obj... so no

•xData to avoid compiler errors

•namespace pollution avoided with xmlns=””

•Binding’s XPath prop used rather than Path

•if external file, even easier

•“GameStat/@Type” would fill list with each GameStat’s Type attribute

Page 19: Last lecture data binding, binding object markup extension bind to any prop with INotifyPropertyChanged, target must be dep. prop. bind to obj... so no

•data provided in XmlNode etc... objects from System.Xml

•so you can use Path and XPath together

•needs hierarchical data template if you want to bind to xml tree