c# - 将工具提示绑定(bind)到 Content Presenter 内容

标签 c# wpf xaml tooltip

我正在设计 TreeViewItem 的样式。该项目有一个内容演示者,我希望它显示一个工具提示:

<ContentPresenter x:Name="PART_Header"
             Cursor="Hand"
             Grid.Column="1"
             ContentSource="Header"
             HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}">
   <ContentPresenter.ToolTip>
       <ToolTip 
                 Placement="RelativePoint" 
                 VerticalOffset="-2" 
                 HasDropShadow="False"
                 BorderBrush="#767676" 
                 Background="#FFF" 
                 Padding="0,1,0,1"
                 VerticalAlignment="Center"
                 Content="{Binding RelativeSource={RelativeSource FindAncestor, 
                 AncestorType={x:Type ContentPresenter}}, Path=Content}"/>
   </ContentPresenter.ToolTip>
</ContentPresenter>

这实际上是设置项目控制模板的较大 setter 的一部分。但是,我似乎无法将工具提示的内容绑定(bind)到内容演示者的内容。它一直显示为空白(一个小黑框)。我是绑定(bind)和 WPF 的新手,所以请原谅。

最佳答案

工具提示和其他弹出窗口存在于主可视化树之外,因此无法使用 RelativeSource 获取父项。尝试使用 ToolTip 的 PlacementTarget 属性来访问其父 ContentPresenter:

Content="{Binding RelativeSource={RelativeSource Self}, Path=PlacementTarget.Content}"

关于c# - 将工具提示绑定(bind)到 Content Presenter 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14617290/

相关文章:

c# - 如何在 UWP 中通过多个页面传递对象

c# - HTML:编码错误

c# - 如何正确使用显式实现的接口(interface)属性和 wpf 可见性?

wpf - Prismv4 : Microsoft supports it or is it their solution?

c# - 为什么 IAuthenticationFilter 方法是异步的?

c# - 如何从azure函数获取json对象的输出?

c# - 如何在启动时删除 App.xaml ResourceDictionary?

WPF Stretch ListBox Height 100% of Grid.Row?

c# - 如何在 XAML WPF 中访问自定义控件的属性

c# - mscorlib.dll 命名空间在 Windows Phone 8.1 XAML 中不起作用