WPF 字符串溢出判断,字符串长度是否超过控件宽度

时间:2023-03-08 22:20:18

TextBloc可以将TextTrimming属性设置为CharacterEllipsis

其他控件可以在控件大小变更或者其他事件上附加下列方法

         private void OnEllipsis(object sender, SizeChangedEventArgs e)
{
FormattedText formattedText = new FormattedText(
this.Header.ToString(),
Thread.CurrentThread.CurrentCulture,
this.FlowDirection,
new Typeface(this.FontFamily, this.FontStyle, this.FontWeight, this.FontStretch),
this.FontSize,
this.Foreground); this.IsEllipsis = formattedText.Width > e.NewSize.Width;
}