Bookmark and Share Share...    Subscribe to this feed Feed   About Christian Moser  

Popular DataBinding Expressions

DataContext Bindings

Each control in WPF has a DataContext property. It's meant to be bound to an object that contains the data to be displayed. The DataContext property is inherited along the logical tree.

Binds object in the DataContext
{Binding Name}
Binds the Name property of the object in the DataContext
{Binding Name.Length}
Binds the Length property of the Name property of the object in the DataContext.

RelativeSource Binding

The relative source mode of the binding extension helps you to bind to an object with an relative relation to you. You don't know your or its abolute position, but you know if it's the previous or next item, two levels above you or of an specific type.

{Binding RelativeSource={RelativeSource Self}}
Bind to your self. Helpful to access your own properties.
{Binding Text, RelativeSource={RelativeSource Self}}
Binds Text property of the target element.
{Binding IsSelected, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}}}
Binds to the IsSelected property of a parent element of type ListBoxItem.

Current Item of Collection Bindings

The slash is a special operator that lets you access the current item of a collection view.

{Binding /}
Binds to the current item of a collection in the data context.
{Binding Customers/}
Binds to the current item of a collection from the Customer property within the data context.
{Binding Customers/Name}
Binds to the Name property of the current item of the Customer collection within the data context.

Last modified: 2009-12-23 07:45:26
Copyright (c) by Christian Moser, 2011.

 Comments on this article

Show all comments
Commented on 2.February 2010
if have examples that more understand.
Commented on 17.February 2010
If learn English understand better :)
Commented on 23.February 2010
I logged in just to say LOL
Commented on 28.February 2010
I have gases
Atul Raina
Commented on 28.March 2010
Commented on 22.April 2010
Current Item? I've not heard of that usage. Very interesting.
Commented on 28.May 2010
More examples
Mike Strobel
Commented on 1.July 2010
Might I suggest adding some examples with type-qualified properties in the binding path? This is sometimes necessary to work around ambiguous matches caused by member name collisions. For example, say you have a 'Customer' class that has two 'Name' properties--one defined by the class itself and another defined by an 'IEntity' interface that Customer implements. To bind to the correct property, you would need to qualify it with the declaring type:

{Binding Path=(entities:Customer.Name).Length}
{Binding Path=(entities:IEntity.Name).Length}

This syntax is especially useful if you are using a tool like ReSharper, which can provide smart completion for PropertyPath expressions in Xaml, but only if the first property in the path can be resolved at design time. You also get some level of compile-time verification that your PropertyPath expressions are correct.
Commented on 8.July 2010
Binding a System.Data.DataTable

{Binding Path=[my_column_name]}
Commented on 1.September 2010
Nice post
kiran more
Commented on 15.February 2011
very nice examples
Commented on 4.March 2011
Is possible a binding to a static property?
Commented on 6.May 2011
why it's look different then what i know?but thanks for the post

E-Mail (optional)