使用 aDataTrigger
并绑定 per ElementName
:
<StackPanel>
<TextBlock>
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=cmbSystemVoltage, Path=IsKeyboardFocusWithin}"
Value="True">
<Setter Property="Text"
Value="{Binding ElementName=cmbSystemVoltage, Path=ToolTip}" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
<ComboBox Name="cmbSystemVoltage" ToolTip="RMS value of phase-phase voltage in kV" />
</StackPanel>
编辑
如果您想在TextBlock
我宁愿订阅的多个控件中显示工具提示PreviewGotKeyboardFocus Event
:
<Window PreviewGotKeyboardFocus="Window_PreviewGotKeyboardFocus">
<StackPanel>
<TextBlock x:Name="toolTipIndicator" />
<ComboBox ToolTip="Sample text" />
<TextBox ToolTip="Other sample text" />
</StackPanel>
</Window>
.
void Window_PreviewGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
FrameworkElement element = e.NewFocus as FrameworkElement;
if (element != null && element.ToolTip != null)
{
this.toolTipIndicator.Text = element.ToolTip.ToString();
}
else
{
this.toolTipIndicator.Text = string.Empty;
}
}