如果需要,显示 WPF 工具提示

Show WPF Tooltip if needed(如果需要,显示 WPF 工具提示)
本文介绍了如果需要,显示 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 工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)