4

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.

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the clarification. And the DataContext of TextBlock should be the same as its parent's if not specified?
@Helic, yes. The value of the DataContext is inherited by child elements, unless explicitly replaced.
also if I define a control template to replace the default display template for a control, what is the data context for textblock?<dxe:ComboBoxEdit.DisplayTemplate> <ControlTemplate> <TextBlock Text="{Binding RowData.Row.Cities3, Converter={StaticResource TestConverter}}"></TextBlock> </ControlTemplate> </dxe:ComboBoxEdit.DisplayTemplate>
@Helic, it depends on where the control is instantiated. The control template doesn't change anything about that. But anyway, in most cases a control template shouldn't try to use the DataContext. That's what DataTemplates are here for.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.