【发布时间】:2014-05-08 16:39:44
【问题描述】:
我在 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[] { ' ', ',' }) + "...";
}
}
【问题讨论】:
-
如果文本太长,您应该使用转换器来转换文本。
标签: c# xaml windows-phone-8 windows-phone