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 ToolTip
the most simple way is this:
<TextBlock Text="Test"
ToolTipService.ToolTip="Test"
/>
So that the cursor is suspended TextBlock
is 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 TextBlock
enough to show all the text content, the display Tooltip
is clearly superfluous things. But UWP and failed to carry out this common automatic processing, such will TextBlock
automatically appear in the text overflow Tooltip
as a default behavior, so we need to realize that their own operations.
Thinking
I can think of ideas by means of TextBlock.IsTextTrimmed
property, in True
the Tooltip when setting the value TextBlock.Text
at False
the time of setting Tooltip
the value of null
.
But when actually created, I find it difficult to do the following reasons:
- Converter The
ConverterParameter
property is a simple object (object), can not be passed by value bindings (onlyDependencyProperty
to use binding), which means I can not bindIsTextTrimmed
by simultaneousConverterParameter
incoming value of the Text property - I can not bind directly in the Converter
TextBlock
itself, the goal is too big, and I just want toIsTextTrimmed
be judged when the property changes.
In summary, after finding some information, I decided to transform the look Converter
itself.
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 DependencyProperty
is used to store TextBlock.Text
value.
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.