WPF Data Binding Cheat Sheet
So as I was scouring the web for the best way to access a parent data context from within an ItemsControl Item Template. I found this fantastic cheat sheet for WPF binding. Of course it didn’t have exactly what I was looking for but it is a great reference.
<Button
Content="Move Up"
Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}},
Path=DataContext.MyCommand}"
CommandParameter="{Binding}"
/>
The above XAML lives within a Item Template and accesses the DataContext from the parent element.
WPF Binding CheatSheet v1.1
Find the latest version at go.nbdtech.com?94E138EA
Part I – Common Examples
Basic Binding
-
{Binding}: Bind to currentDataContext. -
{Binding Name}: Bind toNameproperty of currentDataContext. -
{Binding Name.Length}: Bind toLengthproperty inName. -
{Binding ElementName=SomeTextBox, Path=Text}: Bind toTextofSomeTextBox.
XML Binding
-
{Binding Source={StaticResource BooksData} XPath=/books/book}: Bind XPath query. -
{Binding XPath=@name}: Bind XPath query to XML node inDataContext.
Relative Source Binding
-
{Binding RelativeSource={RelativeSource Self}}: Bind to target element. -
{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=Title}: Bind to parent window’s title.
Collection Current Item Binding
-
{Binding /}: Bind to current item inDataContext. -
{Binding AllItems/Name}: BindNameof current item inAllItems.
Part II – Binding Properties Overview
| Property | Description |
|---|---|
BindingGroupName |
Used for validating multiple bindings together. |
BindsDirectlyToSource |
Bind to the data source provider object. |
Converter |
Converter for data transformation. |
ElementName |
Binds to element by name. |
Mode |
Binding direction (e.g., TwoWay, OneWay). |
Path |
Source property path. |
RelativeSource |
Sets source relative to target element. |
Source |
Explicit object as source. |
StringFormat |
Format string for display. |
UpdateSourceTrigger |
Controls update timing of source. |
ValidatesOnDataErrors |
Use IDataErrorInfo for validation. |
Internationalization Fix: Use FrameworkElement.LanguageProperty.OverrideMetadata to respect user-selected culture settings.
For detailed examples, please refer to the full PDF here.