[] UWP only displayed when TextBlock Tooltip text overflow

Original: [UWP] is only displayed when TextBlock Tooltip text overflow

Foreword

This is what I came across today while answering SO questions, that may be a more common, it is recorded for reference.

Usually, we use ToolTipthe most simple way is this:

<TextBlock Text="Test"
           ToolTipService.ToolTip="Test"
           />

So that the cursor is suspended TextBlockis over, it will display a tooltip, but this seems to be contrary to the principle of a design:

As an alternative to the elements when ToolTip As a reminder, the current contents of the display should only incomplete, and users are willing to view the full content

This is well understood, if TextBlockenough to show all the text content, the display Tooltipis clearly superfluous things. But UWP and failed to carry out this common automatic processing, such will TextBlockautomatically appear in the text overflow Tooltipas a default behavior, so we need to realize that their own operations.


Thinking

I can think of ideas by means of TextBlock.IsTextTrimmedproperty, in Truethe Tooltip when setting the value TextBlock.Textat Falsethe time of setting Tooltipthe value of null.

But when actually created, I find it difficult to do the following reasons:

  1. Converter The ConverterParameterproperty is a simple object (object), can not be passed by value bindings (only DependencyPropertyto use binding), which means I can not bind IsTextTrimmedby simultaneous ConverterParameterincoming value of the Text property
  2. I can not bind directly in the Converter TextBlockitself, the goal is too big, and I just want to IsTextTrimmedbe judged when the property changes.

In summary, after finding some information, I decided to transform the look Converteritself.

Solution

Converter

public class TrimConverter : DependencyObject, IValueConverter
{
    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register("Text", typeof(string), typeof(TrimConverter), new PropertyMetadata(""));


    public object Convert(object value, Type targetType, object parameter, string language)
    {
        bool isTrim = System.Convert.ToBoolean(value);
        return isTrim ? Text : null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

I created within a Converter DependencyPropertyis used to store TextBlock.Textvalue.

use

<Page.Resources>
    <local:TrimConverter x:Key="TrimConverter" Text="{Binding ElementName=TestBlock,Path=Text}"/>
</Page.Resources>

...
<TextBlock MaxWidth="100" TextTrimming="CharacterEllipsis"
           x:Name="TestBlock"
           ToolTipService.ToolTip="{Binding ElementName=TestBlock, 
                                            Path=IsTextTrimmed,
                                            Converter={StaticResource TrimConverter}}"/>

After completion of the binding in XAML interface, the measurement can solve my needs.

But this solution is not perfect, it has a problem:

And coupling TextBlock itself, due to the need to bind the Text value, only a TextBlock create a Converter, can no longer use


Implement displayed in the TextBlock text overflow Tooltip be implemented in many ways, I only propose a for reference.

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/11963523.html