I am trying to do some simple binding to a property inside my usercontrol. Does anyone know why this doesn't work? It works when the TextBlock is outsid开发者_Go百科e the Tooltip.
Thanks!
MasterPage.cs:
MyUserControlInstance.DisplayName = "Test";
MyUserControl.xaml
<ToolTipService.ToolTip>
<ToolTip Template="{StaticResource ToolTipTemplate}">
<StackPanel>
<TextBlock Text="{Binding ElementName=UserControl, Path=DisplayName}" />
</StackPanel>
</ToolTip>
</ToolTipService.ToolTip>
MyUserControl.cs
public static DependencyProperty DisplayNameProperty = DependencyProperty.Register("DisplayName", typeof(string), typeof(MyUserControl));
public string DisplayName
{
get { return (string)GetValue(DisplayNameProperty); }
set { SetValue(DisplayNameProperty, value); }
}
The Tooltip has PlacementTarget property that specifies the UI element that has the Tooltip
<TextBlock.ToolTip>
<ToolTip
DataContext="{Binding Path=PlacementTarget, RelativeSource={x:Static RelativeSource.Self}}"
<TextBlock Text="{Binding Text}"> <!-- tooltip content -->
</ToolTip>
</TextBlock.ToolTip>
Element to Element Binding is not working for ToolTips, because tooltip has it's own element tree. Here is one way to do it
A tool tip is considered to be a control in its own right and therefore cant see its direct parent. The binding inside it cant access the UserControl Element as it knows nothing about it. See here for a couple of solutions to this problem.
精彩评论