微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

如果发生这种情况,我想调整所选的高度,以便不切断线条.这是我到目前为止的代码

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

                if (requiredHeightAdjustment != 0)
                {
                    this.Height -= requiredHeightAdjustment;
                }

– 编辑 –

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

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;
}

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

解决方法

由于LineHeight依赖于字体系列和字体大小,因此您可能需要查看.NET 4.0中的 GlythTypeface类.虽然它可能有点太低,但它有像Height这样的属性

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐