I have in my window a rectangle with a tooltip, Clicking the button suppose to change the tooltip text but it doesn't.
XAML:
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.Resources>
<ToolTip x:Key="@tooltip">
<TextBlock Text="{Binding Name}" />
</ToolTip>
</Grid.Resources>
<Rectangle Width="200" Height="200" Fill="LightBlue" VerticalAlignment="Center" HorizontalAlignment="Center"
ToolTip="{DynamicResource @tooltip}" />
<Button Click="Button_Click" Grid.Row="1" Margin="20">Click Me</Button>
</Grid>
code behind:
public partial class Window1 : Window
{
public Window1()
{
DataContext = new Person { Name = "A" };
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
DataContext = new Person { Name = "B" };
}
}
Change binding to:
<ToolTip x:Key="@tooltip">
<TextBlock Text="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Parent.PlacementTarget.DataContext.Name}" />
</ToolTip>
The reason this works and yours doesn't is because the TextBlock element never gets notified of a change in a property named Name that you bind to in your code.
To fix that, in this example, TextBlock is bound to Parent (ToolTip) > PlacementTarget (Button) > DataContext (Person) > Name property. Here TextBlock has reference to PlacementTarget which in your case is a Button. Button notifies TextBlock of a change in it's DataContext property value on which notification, TextBlock updates itself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With