target: dependency property source: any public property clr and wpf properties target: dependency...
TRANSCRIPT
Bring Your Data to Life with Windows Presentation FoundationAnson TsaoSenior Program ManagerMicrosoft Corporation
Data is Everywhere
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
Basic Data Binding
demo
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} }" />
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}}
Change Notification
demo
Change Notification
Property ChangesINotifyPropertyChanged PropertyChanged event pattern
Collection ChangesINotifyCollectionChangedObseverableCollection<T>BindingList<T>
Binding to a Collection
demo
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>
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
Fancy Data Templates
demo
Collection Views
demo
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
Master-Details
demo
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
Hierarchial Data
demo
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>
What’s new in 3.5
New Debugging MechanismSupport for IDataErrorInfoAlternative Syntax for Data ValidationLINQ Support
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
Questions
© 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.