文章出處
文章列表
WPF TextBlock 設置TextTrimming情況下
判斷 isTextTrimmed(Text 文本是否超出 是否出現了省略號)
private bool HasTextTrimmed(TextBlock textBlock) { Typeface typeface = new Typeface( textBlock.FontFamily, textBlock.FontStyle, textBlock.FontWeight, textBlock.FontStretch); FormattedText formattedText = new FormattedText( textBlock.Text, System.Threading.Thread.CurrentThread.CurrentCulture, textBlock.FlowDirection, typeface, textBlock.FontSize, textBlock.Foreground); formattedText.MaxTextWidth = textBlock.ActualWidth; bool isTrimmed = formattedText.Height > textBlock.ActualHeight || formattedText.Width >textBlock.MaxTextWidth;
return isTrimmed;
}
根據以上屬性,可以做很多事,如文本超出時,設置ToolTip、文本替換等。
文章列表
全站熱搜