[英]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.