2014-05-08 46 views
1

我在Windows Phone 8應用程序中使用了很多元素的長列表選擇器。每個項目都有一個文本塊,每個文本的文本可以從幾個字母到很多單詞。我想保留文本到一行,所以我將TextWrapping屬性設置爲「NoWrap」。我想添加「...」並裁剪文字,如果它太長而無法放在屏幕上。 我到目前爲止都試圖使用每個TextBlock的加載事件並減少文本,直到它適合屏幕。但是,當列表中有許多元素時,加載事件不會爲所有文本塊激活。有沒有合適的方法來解決這個問題?字符串太長時,中斷來自文本塊的文本塊「...」

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[] { ' ', ',' }) + "..."; 

     } 
    } 
+5

[TextTrimming](http://msdn.microsoft.com/en-us/library/system.windows.controls.textblock.texttrimming%28v=vs.110%29.aspx) –

+0

您應該使用Converter to如果文本太長,請轉換文本。 –

回答

1

下面是如何實現這樣的轉換例如:

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}"/> 

A漂亮,可重複使用的解決方案,您可以添加到您的轉換器集合。