zoukankan      html  css  js  c++  java
  • [WPF]TextTrimming截断后,ToolTip显示完整信息

    文本过长被截断后,用ToolTip显示完整信息。

    文本未被截断,则不显示ToolTip。

      

    值转换器:

    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:

    <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>

    参考:http://stackoverflow.com/questions/6342146/show-tooltip-when-text-is-being-trimmed

  • 相关阅读:
    js以字符串方式创建DOM(原生js,jquery,extjs)
    gallery3
    检测标准类型和内置对象类型
    js数据类型和类型检测
    gallery2
    gallery
    如何使用Git上传项目代码到github
    sublime EMMET
    模糊搜索
    导出表格
  • 原文地址:https://www.cnblogs.com/youliao/p/6221625.html
Copyright © 2011-2022 走看看