Thank you Sergey.
Indeed, setting the DataContext on the TextBlock make the tooltip to work:
<Button x:Name="btn" Content="Caption" Width="100" Height="30">
<ToolTipService.ToolTip>
<TextBlock x:Name="txtBlock" Text="{Binding Name}"/>
</ToolTipService.ToolTip>
</Button>
namespace TooltipDataBindingTest
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
Person person = new Person();
person.Name = "Andrew";
this.txtBlock.DataContext = person;
}
}
}
But I still don't understand and I am confused.
Why when using ToolTipService.ToolTip attached property it DOES work:
<UserControl x:Class="TooltipDataBindingTest.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
<Button x:Name="btn" Content="Caption" Width="100" Height="30" ToolTipService.ToolTip="{Binding Name}" />
</Grid>
</UserControl>
but when using a custom layout it does NOT work:
<UserControl x:Class="TooltipDataBindingTest.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
<Button x:Name="btn" Content="Caption" Width="100" Height="30">
<ToolTipService.ToolTip>
<TextBlock Text="{Binding Name}" />
</ToolTipService.ToolTip>
</Button>
</Grid>
</UserControl>
Both of them use the same way of data binding:
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
Person person = new Person();
person.Name = "Andrew";
this.btn.DataContext = person;
}
}
I expect either both to work or not work in the same time. Why they are working differently ?
Please click on 'Mark as answer' near my comment if you feel I answered your question.