[英]Dynamic TextBlock font size in Silverlight
我正在將一個TextBlock包裝在Viewbox中,正如StackOverflow上的幾個答案所示,這就像我想要的那樣呈現。 但經過一些研究后,我明白這會妨礙性能,特別是當你有大量使用這種方法的TextBlock時。
在Silverlight中有更好的方法嗎?
注意:只要我可以顯示文本,我不介意使用TextBlock以外的東西。
// Event handler
private void ControlsSizeChanged(object sender, System.Windows.SizeChangedEventArgs e)
{
GetFontSize(sender as Control);
}
// Method for font size changes
public static void GetFontSize(Control control)
{
PropertyInfo info;
if (control == null || control.ActualHeight <= 0)
return;
if(( info = control.GetType().GetProperty("FontSize", typeof(double))) != null)
{
info.SetValue(control, 0.7 * control.ActualHeight, null);
}
}
沒有ViewBoxes只涉及魔術計算。 線程中還有其他建議,例如更改字體大小和測量幾次,直到文本適合。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.