This commit is contained in:
NaBian committed 2021-05-26 22:04:27 +08:00
1 parent aded2ab4e3
commit c9fb7a2eb5
6 files changed
+57 -49

No files matched your search

@@ -1,6 +1,8 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using HandyControl.Data;
using HandyControl.Tools.Helper;
namespace HandyControl.Controls
{
@@ -15,13 +17,11 @@ namespace HandyControl.Controls
{
if ((bool) e.NewValue)
{
ctl.TextTrimming = TextTrimming.WordEllipsis;
UpdateTooltip(ctl);
ctl.SizeChanged += TextBlock_SizeChanged;
}
else
{
ctl.ClearValue(TextBlock.TextTrimmingProperty);
ctl.SizeChanged -= TextBlock_SizeChanged;
}
}
@@ -46,8 +46,23 @@ namespace HandyControl.Controls
textBlock.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
var width = textBlock.DesiredSize.Width - textBlock.Margin.Left - textBlock.Margin.Right;
ToolTipService.SetToolTip(textBlock,
textBlock.RenderSize.Width > width || textBlock.ActualWidth < width ? textBlock.Text : null);
if(textBlock.RenderSize.Width > width || textBlock.ActualWidth < width || CalcTextWidth(textBlock) > width)
{
ToolTipService.SetToolTip(textBlock, textBlock.Text);
}
else
{
ToolTipService.SetToolTip(textBlock, null);
}
}
private static double CalcTextWidth(TextBlock textBlock)
{
var formattedText = TextHelper.CreateFormattedText(textBlock.Text, textBlock.FlowDirection,
new Typeface(textBlock.FontFamily, textBlock.FontStyle, textBlock.FontWeight, textBlock.FontStretch),
textBlock.FontSize);
return formattedText.WidthIncludingTrailingWhitespace;
}
}
}