Wpf- 如何在代码中获取普通 TextBox 的 LineHeight?

Jus*_*tin 5 c# wpf textbox

我正在开发一个CustomControl继承自TextBox并可以通过Ctrl在拖动鼠标时按住来调整大小的方法,但有时当你调整它的大小时,线条会像这样被切断:

在此处输入图片说明

如果发生这种情况,我想调整选择的高度,这样线条就不会被切断。这是我到目前为止的代码:

double LineHeight = ??;
double requiredHeightAdjustment = this.Height % LineHeight; 

                if (requiredHeightAdjustment != 0)
                {
                    this.Height -= requiredHeightAdjustment;
                }
Run Code Online (Sandbox Code Playgroud)

- 编辑 -

如果将来有人需要这个,这就是我最终得到的:

double fontHeight = this.FontSize * this.FontFamily.LineSpacing;

double requiredHeightAdjustment = this.Height % fontHeight;

var parent = this.Parent as FrameworkElement;

if (requiredHeightAdjustment != 0)
{
    double upwardAdjustedHeight = (fontHeight - requiredHeightAdjustment) + this.Height;

    if (requiredHeightAdjustment >= fontHeight / 2 && this.MaxHeight >= upwardAdjustedHeight
        && (parent == null || parent.ActualHeight >= upwardAdjustedHeight))
        this.Height = upwardAdjustedHeight;
    else
        this.Height -= requiredHeightAdjustment;
}
Run Code Online (Sandbox Code Playgroud)

此解决方案还会对所选TextBox大小进行最小的必要更改,而不是始终进行负面更改。

Phi*_*mid 2

由于您的 LineHeight 取决于字体系列和字体大小,因此您可能需要查看.NET 3.0+ 中的GlyphTypeface类。虽然它可能有点太低级了,但它具有诸如Height.