Asha
Asha

Reputation: 4311

WPF - databinding to a property of same control

I have a control (let's say a textbox) and I want bind the value of one property (let's say tooltip) to value of another property in same control(let's say text).

i want something like belowing but I dont know how can I bind the tooltip to text of same control :

<textBox text="abc" tooltip={Binding ???} />

Upvotes: 14

Views: 7864

Answers (2)

ThomasAndersson
ThomasAndersson

Reputation: 1904

Use RelativeSource:

<TextBox Text="abc" ToolTip="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Text}" />

Upvotes: 39

Jackson Pope
Jackson Pope

Reputation: 14640

If you use the MVVM pattern you can expose a property on the ViewModel and then bind both to the same property:

<textBox text="{Binding Text}" tooltip="{Binding Text}" />

And in the ViewModel:

public string Text { get return "abc"; }

This allows you to unit test that the value presented is correct.

Upvotes: 1

Related Questions