WPF - Binding Data to Ownership of the Same Control

I have a control (say, a text box), and I want to associate the value of one property (let’s a hint) with the value of another property in the same control (let’s text).

I need something like below, but I don’t know how to attach a tooltip to the text of the same control:

<textBox text="abc" tooltip={Binding ???} />
+5
source share
2 answers

Use RelativeSource:

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

If you use the MVVM pattern , you can set the property in the ViewModel, and then bind both to the same property:

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

And in ViewModel:

public string Text { get return "abc"; }

unit test .

+1

All Articles