Wpf Binding Cheatsheet Version

ADVERTISEMENT

WPF Binding CheatSheet version 1.1
You can always find the latest version of this cheat sheet at
Part I – Common Examples
Basic Binding
{Binding}
Bind to current DataContext.
{Binding Name}
Bind to the “Name” proeprty of the current
DataContext.
{Bindind Name.Length}
Bind to the Length property of the object in the
Name property of the current DataContext.
{Binding ElementName=SomeTextBox, Path=Text}
Bind to the “Text” property of the element XAML
element with name=”SomeTextBox” or
x:Name=”SomeTextBox”.
XML Binding
{Binding Source={StaticResource BooksData}
Bind the result of XPath query “/books/book” from
XPath=/books/book}
the XML in the XmlDataProvider in a parent’s
“Resources” elememt with x:Key=”BooksData”.
{Binding XPath=@name}
Bind to the result of an XPath query run on the XML
node in the DataContext (for example in an
ItemControl’s DataTemplate when the
ItemsControl.ItemsSource is bound to an XML data
source).
Relative Source Binding
{Binding RelativeSource={RelativeSource Self}}
Bind to the target element.
{Binding RelativeSource={RelativeSource Self},
Bind to the “Name” property of the target element.
Path=Name}
{Binding RelativeSource={RelativeSource
Bind to the title of the parent window.
FindAncestor, AncestorType={x:Type Window}},
Path=Title}
nd
{Binding RelativeSource={RelativeSource
Bind the the name of the 2
parent of type
FindAncestor, AncestorType={x:Type
ItemsControl.
ItemsControl}, AncestorLevel=2}, Path=Name}
{Binding RelativeSource={RelativeSource
Inside a control template, bind to the name
TemplatedParent}, Path=Name}
property of the element the template is applied to.
{TemplateBinding Name}
Shortcut for the previous example.
Collection Current Item Binding
{Binding /}
Bind to the current item in the DataContext (when
DataContext is a collection)
{Binding AllItems/}
Bind to the current item in the “AllItems“ property
of the DataContext
{Binding AllItems/Name}
Bind to the “Name” property of the current item in
the “AllItems“ property of the DataContext
* Someone has to manage the collection current item, you can do it in code using the CollectionView class
or set IsSynchronizedWithCurrentItem="True" on a control that supports it (like ListBox) and is bound to
the same collection.

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Business
Go
Page of 3