260 likes | 581 Views
WPF Data Binding. Tomer Shamam Smart Client Team Leader Sela Group tomer@sela.co.il http://blogs.microsoft.co.il/blogs/tomershamam. Agenda. WPF in a Nutshell Today’s Data-View Data Binding Concept Handling Data Changes Value Conversion Collection Data Binding Data Validation
E N D
WPF Data Binding Tomer Shamam Smart Client Team Leader Sela Group tomer@sela.co.il http://blogs.microsoft.co.il/blogs/tomershamam
Agenda • WPF in a Nutshell • Today’s Data-View • Data Binding Concept • Handling Data Changes • Value Conversion • Collection Data Binding • Data Validation • Trace and Debug • Data Visualization • New in .NET 3.5 SP1 • Limitations
Today’s Data-View UI Add to ListBox Register Events Set Data Load Get Data CeateListBoxItem Data Binding Logic Data Changed Set Move Update Collection Data Arrived Get Data Data Register Events <Aquarium> <Thermometer/> <Fish/> <Fish/> </ Aquarium > Database Services XML
Data Binding in Action View Data Binding Target Binding Source Dependency Property Property Data Binding One Way Initialize Target Two Way Update Target Update Source One Time Validate Errors One Way To Source Convert Data
Handling Data Changes Socket socket = …; View view = …; Binding binding = …; ... socket.IsActive = true; IsActive = Socket true false Binding Changed
Property Change Mechanisms • INotifyPropertyChanged interface • <Property>Changed event pattern • PropertyDescriptor value changed • WPF DependencyProperty
Two-way Value Conversion INotifyPropertyChanged IValueConverter Fahrenheit Celsius Converter + Temperature Site Temperature Brush Converter IValueConverter
Collection Data Binding <ListView ItemsSource=“{Binding}” /> SingleSocket Collection<Automation> RangeSwitch Sort () Filter() Group() CollectionView OnOffSwitch SingleSocket
Data Validation • User input validation in WPF is done through Binding • Three options to validate input: • ExceptionValidationRule • IDataErrorInfo • Custom Validation Rule <Binding Source="{StaticResource data}" Path="Age" UpdateSourceTrigger="PropertyChanged"> <Binding.ValidationRules> <ExceptionValidationRule/> </Binding.ValidationRules> </Binding>
Trace and Debug • Binding throws exceptions on critical errors • Path spelling mistakes are traced • Binding errors are traceable C# TraceListener listener = new XxxWriterTraceListener(...); PresentationTraceSources.DataBindingSource.Listeners.Add( listener); <system.diagnostics> <sources> <source name="System.Windows.Data" switchName=“..."> ... </sources> </system.diagnostics> App.config
Data Visualization SingleSocket RangeSwitch Binding OnOffSwitch SingleSocket Collection<Automation>
Limitations • Data Binding is Untyped • Target is limited to DependencyObject • No multithreaded support for Collection Binding • Data Template parts are Hard to Get! • Interface-specific Data Template is not supported • Blend data-binding support is limited
New in .NET 3.5 SP1 • StringFormat support within {{ Binding }} expressions • New alternating rows support in ItemsControl • Better handling for null in editable controls • Item-level validation • MultiSelector support • IEditableCollectionView support • Performance improvements with IEnumerable
Summary • New Design Approach • Data-View Decoupling • Better Development Experience • Eliminates Boilerplate Code • Try it now…
Resources Data Binding on MSDN http://msdn2.microsoft.com/en-us/library/ms750612.aspx Windows Client .NET http://windowsclient.net/ Beatriz Costa Blog http://www.beacosta.com/blog/ My Blog http://blogs.microsoft.co.il/blogs/tomershamam/
Don’t work too hard! Data Binding It works for You! © 2007 Microsoft Corporation. All rights reserved.This presentation is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS SUMMARY.