我需要做一个特殊的文本修剪。假设我的字符串是:abcd
缺省的裁剪会得到这样的结果:ab...
但我想让它成为。a..d
你知道怎么实现它吗?
目前我正在使用
<TextBlock Text="abcdLongWord" TextTrimming="CharacterEllipsis"/>发布于 2013-04-09 20:42:25
我在过去就有过这样的担忧,并编写了自己的转换器来处理科林的过程。
转换器类
internal class KearningConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
string result = value.ToString();
try {
int length = int.Parse(parameter.ToString());
if (result.Length > length) {
result = result.Substring(0, length) + "...";
}
} catch {
result += "...";
}
return result;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
throw new NotImplementedException();
}
}Xaml标记
xmlns:conv="clr-namespace:project.converters;assembly=project"
<Window.Resources>
<conv:KearningConverter x:Key="kearnConverter"/>
</Window.Resources>
<TextBlock Text="{Binding Path=AttributeName, Converter={StaticResource kearnConverter}, ConverterParameter=3}"/>通过这种方式,您可以根据不同的需求,根据您的UI布局来实现kearning的多种实现。
发布于 2013-04-09 19:25:08
没有进行测试,但取决于您实际需要的内容:
string test = "abcdefghij";
int nCharNum = test.Length();
test.Remove(2, nCharNum - 1); // this will get you "aj"
//or
test.Remove(2, 3); // this will result in "adefg..."
//or
test.Remove(2, nCharNum - 1);
test.Insert(2, "..") // this will get you "a..j"希望这能有所帮助。(索引可能需要一些修正)
https://stackoverflow.com/questions/15900194
复制相似问题