Last lecture data binding, binding object markup extension bind to any prop with...

Preview:

Citation preview

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

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.

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

•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

•custom or builtin

•can use a valueconverter

•and pass to ctor

•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

filtering

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

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

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)

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

additional views•CollectionViewSource can be used to create

new views and applied to targets

•binding paths

•remain same

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

•must include another namespace

•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

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

XmlDataProvider

•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

•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

Recommended