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.

[xml]Background=”{Binding ElementName=userMenu,Path=Background}”[/xml]

Example

[xml]<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>[/xml]

Bind to Environment Properties

[xml] Header=”{x:Static s:Environment.UserName}”[/xml]

 

This requieres following namespace declaration.

[xml]xmlns:s=”clr-namespace:System;assembly=mscorlib”[/xml]

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.

[xml]xmlns:properties=”clr-namespace:YOUR_NAMESPACE.Properties”[/xml]

 

Implementation

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

[xml]Title=”{Binding Source={x:Static properties:Settings.Default}, Path=NAME_OF_PROPERTY}”[/xml]

 

Bind to Self Property

[xml]Content=”{Binding RelativeSource={RelativeSource Self}, Path=Text}”[/xml]

 

Bind to Ancestor

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

[xml]{Binding Path=ActualWidth, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window, AncestorLevel=1}}[/xml]

Updated on November 11, 2018

Was this article helpful?

Related Articles

Need Support?
Can’t find the answer you’re looking for? Don’t worry we’re here to help!
Contact Support

Leave a Comment

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

Privacy Preferences

Strictly Necessary

Cookies that are necessary for the site to function properly.

gdpr,PYPF, woocommerce_cart_hash, woocommerce_items_in_cart, _wp_wocommerce_session,gaoop_hide_info,cookieconsent_dismissed, wordpress_,'g-recaptcha-response

Google Adsense (Google Inc.)

We are using Google AdSense to show personalized ads on our webpage and our forum. For more information see the privacy policy by Google.
https://www.google.com/policies/technologies/ads/

Doubleclick, __gads, __gac