我需要进行特殊文本修剪。假设我的字符串是:abcd
默认的修剪会得到这样的结果:ab...
但我需要的是:a..d
有任何想法如何实现它吗?
目前我正在使用
<TextBlock Text="abcdLongWord" TextTrimming="CharacterEllipsis"/>
我需要进行特殊文本修剪。假设我的字符串是:abcd
默认的修剪会得到这样的结果:ab...
但我需要的是:a..d
有任何想法如何实现它吗?
目前我正在使用
<TextBlock Text="abcdLongWord" TextTrimming="CharacterEllipsis"/>
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();
}
}
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}"/>
虽然我没有测试过,但根据您的实际需求,您可能需要:
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"