DataBinding in WPF (Part 2)

During the session that I gave last Saturday a lot of people where suprised that there was more to WPF than just fancy graphics. While explaining the power of databinding I noticed people getting enthusiastic about the possibilities. So I will be posting a couple of Blog posts about Databinding in WPF.

Part 2 : Binding elements to the exposed data

Now that we have our data exposed we can bind it to elements through databinding. Before I show this I thought it would be wise to introduce the concept of WPF databinding.
In WPF you can bind to different types of data. Binding means that the value of a certain property is determined by the value in a given datasource. In the simplest example, this datasource could be a Textbox with the name ‘myTextBox’ and the path to the value could be the Text property.
To tell XAML that you are declaring a binding you must use accolades to tell XAML, ‘watch it, there’s syntax coming up’.
Binding to the Textbox example above is done like this

<TextBlock Text=”{Binding ElementName=myTextBox, Path=Text}”/>

In this case I bind to another element by specifically typing ElementName. Note that you don’t use any quotes inside the accolade part. I could also bind to a relative source, which means I can bind to the first textbox above the current element. This is via a RelativeSource but is out of the scope of this blog entry. We will focus on binding to the data we have exposed in the first part.

To bind to our datacontext we simply make a binding to the complete object. We can do this very easy with an empty Binding syntax

This basically tells the property : Get your data from the first DataContext you encounter. By not specifying a path you tell the binding it should bind to the complete object, in this case, the referenced CollectionView.
Very important here is the IsSynchronizedWithCurrentItem property on the Listbox. This updates the DataContext to move to the selected item in the listbox. All the bound elements will then reflect that change. If we turn this off, nothing happens when selecting a different item.

In the ItemTemplate of the listbox I tell the listbox how to show the items in the list. If I wouldn’t specify any ItemTemplate the listbox would return a simple .ToString() on my CollectionView collection. That’s not what we want so we specify a DataTemplate in which we tell the listbox how the items in the CollectionView should be presented.

In the XAML above I use empty bindings with a specific path. The empty binding tells the binding to bind with the first DataContext it encounters. The path specifies which property it should bind to.

Summary
Now we have our data available and some elements bound to it. In this example we are only binding data from the source (XmlDataProvider) to the target (ListBox). No updates are generated. This is called one-way Binding. Next up is a short overview of the different types of DataBinding available.

0 Responses to “DataBinding in WPF (Part 2)”



  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s




 

September 2008
M T W T F S S
    Dec »
1234567
891011121314
15161718192021
22232425262728
2930  

Follow

Get every new post delivered to your Inbox.