显示工具提示文本被修剪时, [英] Show Tooltip when text is being trimmed

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

问题描述

  <TextBlock Width="100" Text="The quick brown fox jumps over the lazy dog" TextTrimming="WordEllipsis">
     <TextBlock.ToolTip>
        <ToolTip DataContext="{Binding Path=PlacementTarget, RelativeSource={x:Static RelativeSource.Self}}">
           <TextBlock Text="{Binding Text}"/>
        </ToolTip>
     </TextBlock.ToolTip>
  </TextBlock>



我怎样才能显示工具提示只有当文修剪?与Windows desktp快捷方式图标。

How can I show the ToolTip only when the text is trimmed? Like the windows desktp shortcut icons.

推荐答案

工作过Eyjafj的...无论的想法,我来到一个工作,大多声明的解决方案,至少不需要定制控制。第一个障碍要克服在TextBlock的越来越。由于该工具提示呈现的可视树以外,你不能使用的RelativeSource约束力或的ElementName获取在TextBlock的。幸运的是,工具提示类提供通过PlacementTarget属性引用其相关元素。所以,你可以在工具提示的可见性属性绑定到工具提示本身并使用其PlacementTarget属性来访问TextBlock的属性:

Working off of Eyjafj...whatever's idea, I arrived at a working, mostly declarative solution that at least doesn't require a custom control. The first hurdle to overcome is getting at the TextBlock. Because the ToolTip is rendered outside of the visual tree, you can't use a RelativeSource binding or ElementName to get at the TextBlock. Luckily, the ToolTip class provides a reference to its related element via the PlacementTarget property. So you can bind the ToolTip's Visibility property to the ToolTip itself and use its PlacementTarget property to access properties of the TextBlock:

<ToolTip Visibility="{Binding RelativeSource={RelativeSource Self}, Path=PlacementTarget, Converter={StaticResource trimmedVisibilityConverter}}">

下一步是使用一个转换器来看待我们必将以确定文本块工具提示应该是可见或不可见。您可以使用ActualWidth的和DesiredSize做到这一点。 ActualWidth的正是这听起来像;宽度的TextBlock的已经呈现在屏幕上。 DesiredSize是您的TextBlock宁愿是宽度。唯一的问题是,DesiredSize似乎采取TextTrimming考虑,不给你完整的,未修剪的文本的宽度。为了解决这个问题,我们可以重新调用测量方法传递Double.Positive无穷远,实际上,问的TextBlock将有多宽,如果它的宽度没有限制。这将更新DesiredSize属性,然后我们可以做比较:

The next step is using a converter to look at the TextBlock we've bound to to determine if the ToolTip should be visible or not. You can do this using the ActualWidth and the DesiredSize. ActualWidth is exactly what it sounds like; the width your TextBlock has been rendered to on the screen. DesiredSize is the width your TextBlock would prefer to be. The only problem is, DesiredSize seems to take the TextTrimming into account and does not give you the width of the full, untrimmed text. To solve this, we can re-call the Measure method passing Double.Positive infinity to, in effect, ask how wide the TextBlock would be if it its width were not constrained. This updates the DesiredSize property and then we can do the comparison:

textBlock.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));

if (((FrameworkElement)value).ActualWidth < ((FrameworkElement)value).DesiredSize.Width)
    return Visibility.Visible;

这做法实际上说明的here作为一个附加的行为如果你想自动应用它的TextBlocks或不要创建工具提示,将永远是无形浪费资源。下面是我的例子的完整代码:

This approach is actually illustrated here as an attached behavior if you want to apply it automatically to TextBlocks or don't want to waste resources on creating ToolTips that will always be invisible. Here is the full code for my example:

该转换器:

public class TrimmedTextBlockVisibilityConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null) return Visibility.Collapsed;

        FrameworkElement textBlock = (FrameworkElement)value;

        textBlock.Measure(new System.Windows.Size(Double.PositiveInfinity, Double.PositiveInfinity));

        if (((FrameworkElement)value).ActualWidth < ((FrameworkElement)value).DesiredSize.Width)
            return Visibility.Visible;
        else
            return Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}



在XAML:

The XAML:

<UserControl.Resources>
    <local:TrimmedTextBlockVisibilityConverter x:Key="trimmedVisibilityConverter" />
</UserControl.Resources>

....

<TextBlock TextTrimming="CharacterEllipsis" Text="{Binding SomeTextProperty}">
    <TextBlock.ToolTip>
        <ToolTip Visibility="{Binding RelativeSource={RelativeSource Self}, Path=PlacementTarget, Converter={StaticResource trimmedVisibilityConverter}}">
            <ToolTip.Content>
                <TextBlock Text="{Binding SomeTextProperty}"/>
            </ToolTip.Content>
        </ToolTip>
    </TextBlock.ToolTip>
</TextBlock>

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

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