コード例 #1
0
        private static void OnEllipsisPositionChanged(TextBlock textBlock, EllipsisPosition ellipsisPosition)
        {
            var data = TextBlockTrimming.GetData(textBlock);

            data.EllipsisPosition = ellipsisPosition;
            TextBlockTrimming.TrimText(textBlock);
        }
コード例 #2
0
        private static void TextBlock_TextChanged(object sender, EventArgs e)
        {
            var textBlock = (TextBlock)sender;
            var data      = TextBlockTrimming.GetData(textBlock);

            data.OriginalText = textBlock.Text;
            TextBlockTrimming.TrimText(textBlock);
        }
コード例 #3
0
        private static void OnEllipsisPositionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var textBlock = d as TextBlock;

            if (textBlock == null)
            {
                return;
            }
            var ellipsisPosition = (EllipsisPosition)e.NewValue;

            TextBlockTrimming.OnEllipsisPositionChanged(textBlock, ellipsisPosition);
        }
コード例 #4
0
 private static void TextBlock_SizeChanged(object sender, SizeChangedEventArgs e)
 {
     TextBlockTrimming.TrimText((TextBlock)sender);
 }
コード例 #5
0
        private static void TrimText(TextBlock textBlock)
        {
            if (DesignerProperties.GetIsInDesignMode(textBlock))
            {
                return;
            }
            if (textBlock.TextTrimming != TextTrimming.CharacterEllipsis || textBlock.TextWrapping != TextWrapping.NoWrap)
            {
                return;
            }
            // ReSharper disable once CompareOfFloatsByEqualityOperator
            if (textBlock.ActualWidth == 0)
            {
                return;
            }
            using (TextBlockTrimming.BlockTextChangedEvent(textBlock))
            {
                var data = TextBlockTrimming.GetData(textBlock);
                // this actually sets textBlock's text back to its original value
                var desiredSize = textBlock.MeasureString(data.OriginalText);
                if (desiredSize <= textBlock.ActualWidth)
                {
                    return;
                }
                var ellipsisSize = textBlock.MeasureString(ELLIPSIS);
                var freeSize     = textBlock.ActualWidth - ellipsisSize;
                var segments     = new List <string>();
                var epsilon      = ellipsisSize / 3;
                var builder      = new StringBuilder();
                var text         = data.OriginalText;
                switch (data.EllipsisPosition)
                {
                case EllipsisPosition.End:
                    TextBlockTrimming.TrimText(textBlock, text, freeSize, segments, epsilon, false);
                    foreach (var segment in segments)
                    {
                        builder.Append(segment);
                    }
                    builder.Append(ELLIPSIS);
                    break;

                case EllipsisPosition.Start:
                    TextBlockTrimming.TrimText(textBlock, text, freeSize, segments, epsilon, true);
                    builder.Append(ELLIPSIS);
                    foreach (var segment in ((IEnumerable <string>)segments).Reverse())
                    {
                        builder.Append(segment);
                    }
                    break;

                case EllipsisPosition.Middle:
                    var textLength = text.Length / 2;
                    var firstHalf  = text.Substring(0, textLength);
                    var secondHalf = text.Substring(textLength);
                    freeSize /= 2;
                    TextBlockTrimming.TrimText(textBlock, firstHalf, freeSize, segments, epsilon, false);
                    foreach (var segment in segments)
                    {
                        builder.Append(segment);
                    }
                    builder.Append(ELLIPSIS);
                    segments.Clear();
                    TextBlockTrimming.TrimText(textBlock, secondHalf, freeSize, segments, epsilon, true);
                    foreach (var segment in ((IEnumerable <string>)segments).Reverse())
                    {
                        builder.Append(segment);
                    }
                    break;

                default:
                    throw new NotSupportedException();
                }
                textBlock.Text = builder.ToString();
            }
        }