如果需要,显示 WPF 工具提示 [英] Show WPF Tooltip if needed

查看:13
本文介绍了如果需要,显示 WPF 工具提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在有限大小的控件中有一个 TextBlock.如果文本太长而无法放入控件,我想显示一个带有全文的工具提示.这是您肯定从许多应用程序中知道的经典行为.

I have a TextBlock inside a limited-size control. If the text is too long to fit into the control, I'd like to show a tooltip with full text. This is a classic behavior you surely know from many apps.

我尝试使用转换器将 TextBlock 宽度转换为 Tooltip 的可见性.

I tried using a Converter to convert TextBlock width into Tooltip's Visibility.

<GridViewColumn.CellTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding Text}">
            <TextBlock.ToolTip>
                <ToolTip 
                    DataContext="{TemplateBinding Content}" 
                    Visibility="{Binding Converter={StaticResource visConvert}}">

                        <TextBlock Text="{Binding Text}"></TextBlock>
                </ToolTip>
            </TextBlock.ToolTip>
        </TextBlock>
    </DataTemplate>
</GridViewColumn.CellTemplate>

问题在于转换器中:

public object Convert(object value, ...

'value' 是 DataBound 项.我希望值"是 TextBlock,观察其宽度,并将其与 GridViewColumn.Width 进行比较.

'value' is the DataBound item. I'd like the 'value' to be the TextBlock, to observe its Width, and compare it to the GridViewColumn.Width.

推荐答案

我想通了,Tooltip 有 PlacementTarget 属性,该属性指定具有 Tooltip 的 UI 元素.万一有人需要:

I figured it out, the Tooltip has PlacementTarget property that specifies the UI element that has the Tooltip. In case anyone needs it:

<TextBlock Text="{Binding Text}">
    <TextBlock.ToolTip>
        <ToolTip 
             DataContext="{Binding Path=PlacementTarget, RelativeSource={x:Static RelativeSource.Self}}" 
             Visibility="{Binding Converter={StaticResource toolVisConverter}}">
             <TextBlock Text="{Binding Text}"/>  <!-- tooltip content -->
         </ToolTip>
    </TextBlock.ToolTip>
</TextBlock>

然后编写一个将TextBlock转换为Visibility的Converter(基于TextBlock的宽度).

And then write a Converter that converts TextBlock to Visibility (based on TextBlock width).

这篇关于如果需要,显示 WPF 工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆