TextBlock修剪上的自动工具提示

时间:2016-01-11 19:25:34

标签: wpf tooltip textblock trim texttrimming

我已经尝试了一切,

解决方案1:http://siderite.blogspot.com/2010/10/determining-if-textblock-has-been.html

解决方案2:http://blog.scottlogic.com/2011/01/31/automatically-showing-tooltips-on-a-trimmed-textblock-silverlight-wpf.html

我的解决方案:

<TextBlock.ToolTip> 
    <MultiBinding Converter="{StaticResource ToolTipVisibilityWhenTextTrimmedConverter}">                                 
       <Binding ElementName="mytextblock" Path="Text"/>
       <Binding ElementName="mytextblock"/>
       <Binding ElementName="mytextblock" Path="MaxWidth"/>
    </MultiBinding>
</TextBlock.ToolTip>

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
    var text = values[0];
    TextBlock tb = (TextBlock)values[1];
    var maxwidth = values[2];
    var result = CommonMethods.MeasureString(tb);
    bool isTextTrimmed = result.Width > (double)maxwidth;
    var retval = (isTextTrimmed) ? text : null;

    return retval;
}

但是当TextBlock宽度非常接近修剪时,显示ToolTip。有时,当修剪最后2个字符时,ToolTip不会显示。

有什么建议吗?

0 个答案:

没有答案