你可以嘗試使用屬性LineStackingStrategy =「BlockLineHeight」和LineHeight屬性上的Converter以及TextBlock高度上的轉換器。 該轉換器的示例代碼
// Height Converter
public class FontSizeToHeightConverter : IValueConverter
{
public static double COEFF = 0.715;
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return (double)value * COEFF;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
// LineHeightConverter
public class FontSizeToLineHeightConverter : IValueConverter
{
public static double COEFF = 0.875;
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return double.Parse(value.ToString()) * COEFF;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
上轉換器所使用的係數取決於所使用的字體家族(基線和LineSpacing):
<TextBlock Text="ABC" Background="Aqua" LineStackingStrategy="BlockLineHeight"
FontSize="{Binding ElementName=textBox1, Path=Text}"
FontFamily="{Binding ElementName=listFonts, Path=SelectedItem}"
Height="{Binding RelativeSource={RelativeSource Self}, Path=FontSize, Mode=OneWay, Converter={StaticResource FontSizeToHeightConverter1}}"
LineHeight="{Binding RelativeSource={RelativeSource Self}, Path=FontSize, Converter={StaticResource FontSizeToLineHeightConverter}}"/>
![sample with params Coeff = 0.7](https://i.stack.imgur.com/0WhQR.png)
最好的解決辦法是找到如何根據FontFamily的參數Baseline和LineSpacing計算Coeff。 在此示例(Segeo UI)中,高度Coeff = 0.715,LineHeight = 0,875 * FontSize。
您應該使用MeasureString或一些類似的技術。看看這個問題http://stackoverflow.com/questions/824281/wpf-equivalent-to-textrenderer – 2012-03-20 14:16:01