• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Data Bondage in WPF
 

Data Bondage in WPF

on

  • 3,418 views

The "Data Bondage in WPF" presentation from the Toronto Code Camp 2009

The "Data Bondage in WPF" presentation from the Toronto Code Camp 2009

Statistics

Views

Total Views
3,418
Views on SlideShare
3,410
Embed Views
8

Actions

Likes
0
Downloads
31
Comments
0

2 Embeds 8

http://www.slideshare.net 7
http://www.linkedin.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Data Bondage in WPF Data Bondage in WPF Presentation Transcript

    • Bruce Johnson, ObjectSharp Consulting bjohnson@objectsharp.com
    • Data Bondage
    • The Basic Functionality .Title .Text Data Book Class TitleTextBox Binding Target Source Source Data Target Element Property Property Object
    • DataContext Matters • A DataContext in WPF is similar to a DataSource in WinForms/ASP.NET • Provides the basis for data binding Book b = new Book() { Title=quot;The Firmquot;, Author=quot;John Grishamquot; }; this.DataContext = b;
    • Binding Expressions • We saw a simple binding expression in our walkthrough Text=quot;{Binding Path=Title}quot; • Binding expressions can be more complex Text=quot;{Binding Path=DateCreated.Month, Mode=OneWay}quot;
    • Data bindings can have different modes • One way • Two way • One time • One way to source • Default – The characteristics of the target property determines the mode
    • Formatting strings during binding • StringFormat property of Binding class • Formats strings during transfer to target property • Uses standard formatting strings, previously defined in .NET <TextBlock Text=quot;{Binding Path=Price, StringFormat=Only {0:c} with purchase!}quot; />
    • Bindings have silent failure • If a binding fails, no exception is generated • The program’s output will note the binding failure
    • Collections as the DataContext  It’s common to use a list of data items instead of a single one  For example, a collection of books instead of a single book: Private Books As New List(Of Book) Private Sub Window1_Loaded(ByVal sender As Object, … Books.Add( _ New Book(quot;Ender's Gamequot;, quot;Orson Scott Cardquot;)) Books.Add( _ New Book(quot;Old Man's Warquot;, quot;John Scalziquot;)) Me.DataContext = Books End Sub
    • Navigating with a ListBox • Add an attribute to the XAML definition of the Listbox <ListBox ItemsSource=quot;{Binding}quot; DisplayMemberPath=quot;Titlequot; IsSynchronizedWithCurrentItem=quot;Truequot; … > • Listbox now navigates the Books collection If your ListBox fails to navigate the list, always check the IsSynchronizedWithCurrentItem property
    • Lots of possible data sources…  XML data (XPath attribute instead of Path)  ADO.NET Datasets, DataTables  Resource lists defined in XAML You can bind to any control property that is a dependency property IF you can match data types But what if you can’t?
    • Value converters • WPF has some built-in converters – For example, a string name of a color will be converted to a brush • You can write your own converters – Convert one type to another – Reformat strings (if StringFormat not sufficient) • Implement the IValueConverter interface – Convert method – data source property  WPF element property – ConvertBack method – WPF element property  data source property
    • Interesting applications • Bind data field to visibility of element – Built-in BooleanToVisibilityConverter • Bind month number to month name • Bind graphic to data field – Have image vary based on value of data field
    • MultiBinding • A MultiBinding instance binds several data fields in the source to one target property • Collection of Binding instances gets the data fields • Converter assembles and parses the fields
    • Validation during binding • Binding can have a collection of validation rules • Built-in rules handle exceptions and IDataErrorInfo – IDataErrorInfo support is new in FX3.5 SP1 – Not available in Silverlight (even 3) • You can write your own validation rules – Inherit from ValidationRule class – Override the Validate method – Return a ValidationResult object • ValidationResult has a static ValidResult when there's no error
    • Displaying validation errors • By default, all you get is a red rectangle – Yes, I hate it too. It’s ugly. – It can be overridden with a control template – You’ll see this in the Control Templates section • Validation class has a static GetErrors method to return errors on an element • Validation class has an Errors attached property for an element – You can use some fancy binding on this property to display error messages in, e.g., tooltips – However, binding information must be present in every binding that is being validated
    • Another option – Error event • Validation class has an attached event named Error – Set this event on some container that holds all of your fields • Event args tells you if error is being added or removed • Event args includes an Error object with ErrorContent property
    • Binding element-to-element • Use ElementName property of binding instead of setting a DataContext • Allows you to tie user interface elements together • Example – zooming with Slider and TextBox
    • No-code zooming <Grid> <Grid.RowDefinitions> <RowDefinition Height=quot;*quot; /> <RowDefinition Height=quot;8*quot; /> </Grid.RowDefinitions> <StackPanel Name=quot;StackPanel1quot; Orientation=quot;Horizontalquot;> <Slider Height=quot;22quot; Name=quot;ZoomSliderquot; Width=quot;100quot; Minimum=quot;10quot; Maximum=quot;100quot; Value=quot;50quot; /> <TextBox Height=quot;23quot; Name=quot;TextBox1quot; Text=quot;{Binding Path=Value, ElementName=ZoomSlider}quot; Width=quot;120quot; Margin=quot;5quot; /> </StackPanel> <Ellipse Width=quot;{Binding Path=Value, ElementName=ZoomSlider}quot; Height=quot;{Binding Path=Value, ElementName=ZoomSlider}quot; Grid.Row=quot;1quot; Name=quot;Ellipse1quot; Stroke=quot;Blackquot; Fill=quot;Redquot;> </Grid>
    • RelativeSource binding • Allows properties of an element to be bound to other properties of that element Height= quot;{Binding RelativeSource={RelativeSource Self}, Path=Width}quot; • Allows properties of an element to be bound to properties further up in the tree of elements
    • Questions? • My contact information – EMail: bjohnson@objectsharp.com – Twitter: LACanuck – Blog: http://www.objectsharp.com/blogs/bruce – MSN: lacanadians@hotmail.com