Microsoft Dot Net Master

Microsoft Dot Net Master
Microsoft Dot Net Master

Thursday, July 19, 2012

Popular DataBinding Expressions

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.
{Binding}
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.

No comments:

Post a Comment