Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Binding with RelativeSource Self

Tags:

self

binding

wpf

I am confused with whn to use {RelativeSource Self} when binding. The below three bindings look the same to me, where MyText is a property in my view model.

<TextBlock Text = "{Binding Path=MyText RelativeSource{RelativeSource Self} }" />
<TextBlock Text = "{Binding MyText RelativeSource{RelativeSource Self} }" />
<TextBlock Text = "{Binding MyText }" />

When should I use Self in my binding? Thanks.

like image 354
Helic Avatar asked Nov 02 '25 22:11

Helic


1 Answers

The first two are equivalent, and bind to the MyText property of the TextBlock itself. But of course, they don't work, because there is no MyText property on TextBlock.

The third binds to the MyText property of the current DataContext

Use RelativeSource.Self when you need to bind to a property of the current control.

like image 194
Thomas Levesque Avatar answered Nov 04 '25 18:11

Thomas Levesque