Si può provare a utilizzare l'attributo LineStackingStrategy = "BlockLineHeight" e un convertitore sugli attributi LineHeight e un convertitore sull'altezza di TextBlock. Questo è un esempio di codice di convertitori
// 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();
}
}
il coefficiente utilizzato sui convertitori dipende Usato familiari Fonts (Baseline e interlinea):
<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}}"/>
La soluzione migliore è quella di trovare il modo per calcolare il Coeff in base ai parametri Baseline e LineSpacing di FontFamily. In questo esempio (UI Segeo) Coeff of Height = 0.715 e LineHeight = 0,875 * FontSize.
fonte
2013-04-08 10:09:33
È necessario utilizzare MeasureString o una tecnica simile. Guarda questa domanda http://stackoverflow.com/questions/824281/wpf-equivalent-to-textrenderer –