WPF Bindings

Bindings

This is an overview of different binding methods. WPF and Binding can be very confusing for new users. This list will help you for the beginning. I’ll update the list regularly. So if you miss something, or have a binding method you want to have here, just leave a comment!

Bind to Property

Binds to a property of another XAML object.

Background="{Binding ElementName=userMenu,Path=Background}"

Example

<MenuItem x:Name="userMenu"
DockPanel.Dock="Right"
Background="#FF444444"
Foreground="White"

VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"

Height="48"
Header="Username"
>
<MenuItem x:Name="userMenuLogout"
Background="{Binding ElementName=userMenu,Path=Background}"
Foreground="{Binding ElementName=userMenu,Path=Foreground}"
Header="Abmelden"
></MenuItem>
</MenuItem>

Bind to Environment Properties

Header="{x:Static s:Environment.UserName}"

This requieres following namespace declaration.

xmlns:s="clr-namespace:System;assembly=mscorlib"

Bind to Properties.Settings.Default

If you want to bind to a Settings property, you can do it this way:

Requirements

First, you need to add the properties namespace to your XMAL file.

xmlns:properties="clr-namespace:YOUR_NAMESPACE.Properties"

Implementation

In order to access a propertie, insert the following binding code to your XMAL-object attribute, like here, for the title.

Title="{Binding Source={x:Static properties:Settings.Default}, Path=NAME_OF_PROPERTY}"

Bind to Self Property

Content="{Binding RelativeSource={RelativeSource Self}, Path=Text}"

Bind to Ancestor

If you want to bind to a property of the ancestor, implement the binding as following:

{Binding Path=ActualWidth, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window, AncestorLevel=1}}
Updated on February 28, 2021

Was this article helpful?

Related Articles

Not the solution you were looking for?
Click the link below to submit a support ticket
Visist our Forum!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.