加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 21:19:40 所属栏目:百科 来源:网络整理
导读:我正在开发一个继承自TextBox的CustomControl,可以通过按住Ctrl拖动鼠标来重新调整大小,但有时当你重新调整它的大小时,行会像这样被截断: 如果发生这种情况,我想调整所选的高度,以便不切断线条.这是我到目前为止的代码: double LineHeight = ??;double req
我正在开发一个继承自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这样的属性

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读