簡體   English   中英

當字符串太長時,以“…”打斷文本塊中的文本

[英]Interrupt text from a textblock with “…” when string is too long

我在Windows Phone 8應用程序中使用包含許多元素的長列表選擇器。 每個項目都有一個文本塊,並且每個項目的文本可以從幾個字母到很多單詞。 我想將文本保持為一行,因此將TextWrapping屬性設置為“ NoWrap”。 我想添加“ ...”並裁剪文本,如果它太長而無法在屏幕上顯示。 到目前為止,我嘗試使用每個TextBlock的load事件並減少文本,直到它適合屏幕為止。 但是,當列表中包含許多元素時,加載事件不會針對所有文本塊激活。 是否有解決此問題的正確方法?

    private void TextBlock_Loaded_1(object sender, RoutedEventArgs e)
    {
        TextBlock txt = sender as TextBlock;
        if (txt == null)
            return;

        if (txt.ActualWidth > 300)
        {
            while (txt.Text.Length > 4 && txt.ActualWidth > 290)
                txt.Text = txt.Text.Substring(0, txt.Text.Length - 4);
            txt.Text = txt.Text.Substring(0, txt.Text.Length - 3);

            txt.Text = txt.Text.Trim(new Char[] { ' ', ',' }) + "...";

        }
    }

這是如何實現此目的的Converter示例:

using System;
using System.Globalization;
using System.Windows.Data;

namespace PhoneApp2
{
    public class TextLengthConverter: IValueConverter
    {
        #region Implementation of IValueConverter

        /// <summary>
        /// Modifies the source data before passing it to the target for display in the UI.
        /// </summary>
        /// <returns>
        /// The value to be passed to the target dependency property.
        /// </returns>
        /// <param name="value">The source data being passed to the target.</param><param name="targetType">The <see cref="T:System.Type"/> of data expected by the target dependency property.</param><param name="parameter">An optional parameter to be used in the converter logic.</param><param name="culture">The culture of the conversion.</param>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is string)
            {
                int desiredLength;
                if (int.TryParse(parameter.ToString(), out desiredLength))
                {
                    if (desiredLength > 0)
                    {
                        string textIn = value as string;
                        if (textIn.Length > desiredLength // Make sure the truncation is actually needed.
                            && textIn.Length > 3) // Make sure the length if the textIn is longer than the dots so 'something' is shown before the dots.
                        {
                            return textIn.Substring(0, desiredLength - 3) + "...";
                        }
                    }
                }
            }
            return value;
        }

        /// <summary>
        /// Modifies the target data before passing it to the source object.  This method is called only in <see cref="F:System.Windows.Data.BindingMode.TwoWay"/> bindings.
        /// </summary>
        /// <returns>
        /// The value to be passed to the source object.
        /// </returns>
        /// <param name="value">The target data being passed to the source.</param><param name="targetType">The <see cref="T:System.Type"/> of data expected by the source object.</param><param name="parameter">An optional parameter to be used in the converter logic.</param><param name="culture">The culture of the conversion.</param>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}

要在頁面中使用它,請添加資源條目,如下所示:

<phone:PhoneApplicationPage.Resources>
    <PhoneApp2:TextLengthConverter x:Key="TextLengthConverter"/>
</phone:PhoneApplicationPage.Resources>

並將其附加到您的綁定文本中:

<TextBlock Text="{Binding BoundText, Converter={StaticResource TextLengthConverter}, ConverterParameter=4}"/>

您可以將其添加到轉換器集合中,這是一個不錯的,可重用的解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM