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]

Aktualisiert am November 11, 2018

War dieser Beitrag hilfreich?

Verwandte Artikel

Du brauchst Hilfe?
Du kannst die Antwort, nach der du gesucht hast nicht finden? Keine Sorge, wir sind hier um dir zu helfen!
Support kontaktieren

Hinterlasse einen Kommentar

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.