diff --git a/src/Shared/HandyControlDemo_Shared/UserControl/Styles/TextBlockDemoCtl.xaml b/src/Shared/HandyControlDemo_Shared/UserControl/Styles/TextBlockDemoCtl.xaml index 04a1138d..2d652f2d 100644 --- a/src/Shared/HandyControlDemo_Shared/UserControl/Styles/TextBlockDemoCtl.xaml +++ b/src/Shared/HandyControlDemo_Shared/UserControl/Styles/TextBlockDemoCtl.xaml @@ -43,7 +43,7 @@ - + diff --git a/src/Shared/HandyControl_Shared/Controls/Attach/TextBlockAttach.cs b/src/Shared/HandyControl_Shared/Controls/Attach/TextBlockAttach.cs index 4941a4c5..b902534d 100644 --- a/src/Shared/HandyControl_Shared/Controls/Attach/TextBlockAttach.cs +++ b/src/Shared/HandyControl_Shared/Controls/Attach/TextBlockAttach.cs @@ -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; } } } diff --git a/src/Shared/HandyControl_Shared/Controls/Text/OutlineText.cs b/src/Shared/HandyControl_Shared/Controls/Text/OutlineText.cs index 0cdb8217..5c5746bc 100644 --- a/src/Shared/HandyControl_Shared/Controls/Text/OutlineText.cs +++ b/src/Shared/HandyControl_Shared/Controls/Text/OutlineText.cs @@ -1,10 +1,10 @@ using System; using System.ComponentModel; -using System.Globalization; using System.Windows; using System.Windows.Documents; using System.Windows.Media; using HandyControl.Data; +using HandyControl.Tools.Helper; namespace HandyControl.Controls { @@ -201,27 +201,8 @@ namespace HandyControl.Controls return; } -#if NET40 || NET45 || NET451 || NET452 || NET46|| NET461 - _formattedText = new FormattedText( - Text, - CultureInfo.CurrentUICulture, - FlowDirection, - new Typeface(FontFamily, FontStyle, FontWeight, FontStretch), - FontSize, Fill); -#else - var source = PresentationSource.FromVisual(this); - var dpi = 1.0; - if (source?.CompositionTarget != null) - { - dpi = 96.0 * source.CompositionTarget.TransformToDevice.M11 / 96.0; - } - _formattedText = new FormattedText( - Text, - CultureInfo.CurrentUICulture, - FlowDirection, - new Typeface(FontFamily, FontStyle, FontWeight, FontStretch), - FontSize, Fill, dpi); -#endif + _formattedText = TextHelper.CreateFormattedText(Text, FlowDirection, + new Typeface(FontFamily, FontStyle, FontWeight, FontStretch), FontSize); UpdateFormattedText(); } diff --git a/src/Shared/HandyControl_Shared/Controls/Text/SimpleText.cs b/src/Shared/HandyControl_Shared/Controls/Text/SimpleText.cs index 2ecf8aa0..4a5dc9bb 100644 --- a/src/Shared/HandyControl_Shared/Controls/Text/SimpleText.cs +++ b/src/Shared/HandyControl_Shared/Controls/Text/SimpleText.cs @@ -1,10 +1,10 @@ using System; using System.ComponentModel; -using System.Globalization; using System.Windows; using System.Windows.Documents; using System.Windows.Media; using HandyControl.Data; +using HandyControl.Tools.Helper; namespace HandyControl.Controls { @@ -129,27 +129,8 @@ namespace HandyControl.Controls return; } -#if NET40 || NET45 || NET451 || NET452 || NET46|| NET461 - _formattedText = new FormattedText( - Text, - CultureInfo.CurrentUICulture, - FlowDirection, - new Typeface(FontFamily, FontStyle, FontWeight, FontStretch), - FontSize, Foreground); -#else - var source = PresentationSource.FromVisual(this); - var dpi = 1.0; - if (source?.CompositionTarget != null) - { - dpi = 96.0 * source.CompositionTarget.TransformToDevice.M11 / 96.0; - } - _formattedText = new FormattedText( - Text, - CultureInfo.CurrentUICulture, - FlowDirection, - new Typeface(FontFamily, FontStyle, FontWeight, FontStretch), - FontSize, Foreground, dpi); -#endif + _formattedText = TextHelper.CreateFormattedText(Text, FlowDirection, + new Typeface(FontFamily, FontStyle, FontWeight, FontStretch), FontSize); UpdateFormattedText(); } diff --git a/src/Shared/HandyControl_Shared/HandyControl_Shared.projitems b/src/Shared/HandyControl_Shared/HandyControl_Shared.projitems index e7b9e052..34a1414f 100644 --- a/src/Shared/HandyControl_Shared/HandyControl_Shared.projitems +++ b/src/Shared/HandyControl_Shared/HandyControl_Shared.projitems @@ -372,6 +372,7 @@ + diff --git a/src/Shared/HandyControl_Shared/Tools/Helper/TextHelper.cs b/src/Shared/HandyControl_Shared/Tools/Helper/TextHelper.cs new file mode 100644 index 00000000..a1ef12a8 --- /dev/null +++ b/src/Shared/HandyControl_Shared/Tools/Helper/TextHelper.cs @@ -0,0 +1,30 @@ +using System.Globalization; +using System.Windows; +using System.Windows.Media; + +namespace HandyControl.Tools.Helper +{ + internal class TextHelper + { + public static FormattedText CreateFormattedText(string text, FlowDirection flowDirection, Typeface typeface, double fontSize) + { +#if NET40 || NET45 || NET451 || NET452 || NET46 || NET461 + var formattedText = new FormattedText( + text, + CultureInfo.CurrentUICulture, + flowDirection, + typeface, + fontSize, Brushes.Black); +#else + var formattedText = new FormattedText( + text, + CultureInfo.CurrentUICulture, + flowDirection, + typeface, + fontSize, Brushes.Black, DpiHelper.DeviceDpiX); +#endif + + return formattedText; + } + } +}