Helic
Helic

Reputation: 949

WPF Binding with RelativeSource Self

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.

Upvotes: 4

Views: 2627

Answers (1)

Thomas Levesque
Thomas Levesque

Reputation: 292685

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.

Upvotes: 3

Related Questions