wpf Textblock 文字过长时,中间用省略号代替。

如果是省略号加载最后,可直接用TextTrimming属性,如果不加在最后,想将省略号加在中间,提供两种思路。

第一种,利用三个TextBlock(txb、txb1、txb2)来实现,这种显示出来的文字可能有半个字的现象。

txb用来显示不过长的文本,txb1和txb2用来显示过长时,文本的前后两部分,txb1和txb2的可见性和 txb相反。

三者的样式(txb和txb1要设置TextTrimming,txb2要设置HorizontalAlignment为right)

 
            
            
            

当txb大小变化时,要判断是否文字过长,如果过长则显示txb1和txb2,否则显示txb

private void txb_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            TextBlock textBlock = sender as TextBlock;
            if (textBlock != null)
            {
                string str = textBlock.Text;
                Typeface typeface = new Typeface(
           textBlock.FontFamily,
           textBlock.FontStyle,
           textBlock.FontWeight,
           textBlock.FontStretch);
                FormattedText formattedText = new FormattedText(
                    str,
                    System.Threading.Thread.CurrentThread.CurrentCulture,
                    textBlock.FlowDirection,
                    typeface,
                    textBlock.FontSize,
                    textBlock.Foreground);
                if (textBlock.ActualWidth + 1 < formattedText.Width)
                {
                    textBlock.Visibility = Visibility.Hidden;
                }
                else
                {
                    textBlock.Visibility = Visibility.Visible;
                }
            }
        }

第二种,后台来生成适合Textblock大小的文字。

       
            

利用二分法找到要截取的字符串

private void txb_SizeChanged1(object sender, SizeChangedEventArgs e)
        {
            TextBlock textBlock = sender as TextBlock;
            if (textBlock != null)
            {
                string str = textBlock.Text;
                Typeface typeface = new Typeface(
           textBlock.FontFamily,
           textBlock.FontStyle,
           textBlock.FontWeight,
           textBlock.FontStretch);
                FormattedText formattedText = new FormattedText(
                    str,
                    System.Threading.Thread.CurrentThread.CurrentCulture,
                    textBlock.FlowDirection,
                    typeface,
                    textBlock.FontSize,
                    textBlock.Foreground);
                if (textBlock.ActualWidth + 1 < formattedText.Width)
                {
                    string str1 = textBlock.Text;
                    int p1 = findPosition(str1, typeface, textBlock);
                    char[] ret = str1.ToCharArray();
                    string str2 = string.Concat(ret.Reverse());
                    int p2 = findPosition(str2, typeface, textBlock);
                    string strOutPut = str.Substring(0, p1) + "..." + str.Substring(str.Length - p2, p2);
                    txb12.Text = strOutPut;
                }
                else
                {
                    txb12.Text = txb11.Text;
                }
            }

        }

        private int findPosition(string str, Typeface typeface, TextBlock textBlock)
        {
            int start = 0, end = str.Length - 1;
            while (start <= end)
            {
                int mid = (start + end) / 2;
                string strTemp = str.Substring(0, mid);
                FormattedText formattedText = new FormattedText(
                    strTemp,
                    System.Threading.Thread.CurrentThread.CurrentCulture,
                    textBlock.FlowDirection,
                    typeface,
                    textBlock.FontSize,
                    textBlock.Foreground);
                if (formattedText.Width <= (txb11.ActualWidth - 10) / 2)
                    start = mid + 1;
                else
                    end = mid - 1;
            }
            if (start == 0)
                return -1;
            else
                return start - 1;
        }

显示效果

wpf Textblock 文字过长时,中间用省略号代替。_第1张图片

代码路径

https://download.csdn.net/download/bornonew/10579185

你可能感兴趣的:(c#)