target: dependency property source: any public property clr and wpf properties target: dependency...

23
Bring Your Data to Life with Windows Presentation Foundation Anson Tsao Senior Program Manager Microsoft Corporation

Upload: samantha-wilcox

Post on 28-Dec-2015

251 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Target: dependency property Source: any public property CLR and WPF properties Target: dependency property Source: any public property CLR and WPF properties

Bring Your Data to Life with Windows Presentation FoundationAnson TsaoSenior Program ManagerMicrosoft Corporation

Page 2: Target: dependency property Source: any public property CLR and WPF properties Target: dependency property Source: any public property CLR and WPF properties

Data is Everywhere

Page 3: Target: dependency property Source: any public property CLR and WPF properties Target: dependency property Source: any public property CLR and WPF properties

What is Data Binding?

Target: dependency propertySource: any public property

CLR and WPF properties

Target: dependency propertySource: any public property

CLR and WPF properties

Control

“Data Item” Source

Property

Binding

Targ

et

Pro

perty

ADO.NETXML

CLR ObjectsLINQ

Web ServicesWPF Elements

Page 4: Target: dependency property Source: any public property CLR and WPF properties Target: dependency property Source: any public property CLR and WPF properties

Basic Data Binding

demo

Page 5: Target: dependency property Source: any public property CLR and WPF properties Target: dependency property Source: any public property CLR and WPF properties

Object Data Providers

Add to resource dictionaryNamed source objects

Use with resource binding{StaticResource solarSystem}

<Window> <Window.Resources> <ObjectDataProvider x:Key=“solarSystem” ObjectType=“{x:Type l:SolarSystem}" /> </Window.Resources>

...

<TextBlock TextContent="{Binding Path=Sun.Name, Source={StaticResource solarSystem} }" />

Page 6: Target: dependency property Source: any public property CLR and WPF properties Target: dependency property Source: any public property CLR and WPF properties

Text= {Binding Path=Sun.Name Source={StaticResource solarSystem}}

Source= {Binding Path=Sun.Image Source={StaticResource solarSystem}}

Share Common Source

StackPanel

Image

TextBox

DataContext= {Binding Path=Sun Source={StaticResource solarSystem}}

Text= {Binding Path=Name}

Source= {Binding Path=Image}

DataContext= {Binding Path=SunAndPlanets[3] Source={StaticResource solarSystem}}

Page 7: Target: dependency property Source: any public property CLR and WPF properties Target: dependency property Source: any public property CLR and WPF properties

Change Notification

demo

Page 8: Target: dependency property Source: any public property CLR and WPF properties Target: dependency property Source: any public property CLR and WPF properties

Change Notification

Property ChangesINotifyPropertyChanged PropertyChanged event pattern

Collection ChangesINotifyCollectionChangedObseverableCollection<T>BindingList<T>

Page 9: Target: dependency property Source: any public property CLR and WPF properties Target: dependency property Source: any public property CLR and WPF properties

Binding to a Collection

demo

Page 10: Target: dependency property Source: any public property CLR and WPF properties Target: dependency property Source: any public property CLR and WPF properties

Data Templates

class SolarSystemObject{ Uri Image {get;set} string Name {get;set}}

<DataTemplate x:Key="BetterSunAndPlanetsTemplate">

<Border> <StackPanel> <Image Source="{Binding Path=Image}" />

<TextBlock Text="{Binding Path=Name}" /> </Border> </StackPanel> </Border></DataTemplate>

<ControlTemplate><Grid> <ContentPresenter/></Grid>

</ControlTemplate>

Page 11: Target: dependency property Source: any public property CLR and WPF properties Target: dependency property Source: any public property CLR and WPF properties

Choosing a Data Template

By name (resource dictionary key)ContentTemplate/ItemTemplate= {StaticResource myStyle}

By type<DataTemplate DataType="{x:Type cc:Sun}"><DataTemplate DataType="Book"> for XML

Page 12: Target: dependency property Source: any public property CLR and WPF properties Target: dependency property Source: any public property CLR and WPF properties

Fancy Data Templates

demo

Page 13: Target: dependency property Source: any public property CLR and WPF properties Target: dependency property Source: any public property CLR and WPF properties

Collection Views

demo

Page 14: Target: dependency property Source: any public property CLR and WPF properties Target: dependency property Source: any public property CLR and WPF properties

Collection Views

Wraps the Underlying CollectionSortingFilteringGroupingSelection

ItemsControl create default CollectionViewCollectionViewSource creates a CollectionView in XAML

Allows Grouping, Sorting etc in MarkupContains Source and View Properties

Not available in Silverlight 2

Page 15: Target: dependency property Source: any public property CLR and WPF properties Target: dependency property Source: any public property CLR and WPF properties

Master-Details

demo

Page 16: Target: dependency property Source: any public property CLR and WPF properties Target: dependency property Source: any public property CLR and WPF properties

Master Details

Use ItemsControl (e.g. ListBox) as master

Set IsSynchronizedWithCurrentItem="True"

Other bindings on same source will follow master

Alternative: bind to selected item/value

{Binding Path=SelectedValue.Name, ElementName=myListBox }Fragile on UI changes

Page 17: Target: dependency property Source: any public property CLR and WPF properties Target: dependency property Source: any public property CLR and WPF properties

Hierarchial Data

demo

Page 18: Target: dependency property Source: any public property CLR and WPF properties Target: dependency property Source: any public property CLR and WPF properties

Hierarchical Binding<Foo xmlns=''> <Quux> <Wibble /> </Quux> <Bar /> <Spong> <Ick /> <Iyck/> <Baz /> </Spong></Foo>

<TreeView ItemsSource="{Binding XPath='/*'}"> <TreeView.ItemTemplate> <HierarchicalDataTemplate ItemsSource="{Binding XPath='*'}"> <TextBlock Text="{Binding Path=Name}"/> </HierarchicalDataTemplate> </TreeView.ItemTemplate></TreeView>

Page 19: Target: dependency property Source: any public property CLR and WPF properties Target: dependency property Source: any public property CLR and WPF properties

What’s new in 3.5

New Debugging MechanismSupport for IDataErrorInfoAlternative Syntax for Data ValidationLINQ Support

Page 20: Target: dependency property Source: any public property CLR and WPF properties Target: dependency property Source: any public property CLR and WPF properties

Summary

WPF understands your dataUse your favorite data model: XML, objects, SQL, LINQ, WS, …Bind data to controls

Templates give your data a face

Controls “think” data Data is pervasive across and integrated into the platform

Page 21: Target: dependency property Source: any public property CLR and WPF properties Target: dependency property Source: any public property CLR and WPF properties

Questions

Page 22: Target: dependency property Source: any public property CLR and WPF properties Target: dependency property Source: any public property CLR and WPF properties

© 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.

The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after

the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Page 23: Target: dependency property Source: any public property CLR and WPF properties Target: dependency property Source: any public property CLR and WPF properties