我需要做一个特殊的文本修剪。假设我的字符串是:abcd
默认修剪会给我这个:ab...
但我需要它。 a..d
知道如何实现吗?
目前我正在使用
<TextBlock Text="abcdLongWord" TextTrimming="CharacterEllipsis"/>
最佳答案
我过去有过这种担忧,并编写了自己的转换器来处理 Kearning 的过程。
转换器类
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 布局的不同要求实现多个学习。
关于c# - WPF 自定义 TextTrimming,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15900194/