[英]Wpf- How can I get the LineHeight of a normal TextBox in code?
I'm working on a CustomControl
that inherits from TextBox
and can be re-sized by holding Ctrl
while dragging the mouse, but sometimes when you re-size it, lines get cut off like this:我正在开发一个继承自
TextBox
的CustomControl
,可以通过在拖动鼠标时按住Ctrl
来重新调整大小,但有时当您重新调整大小时,线条会像这样被切断:
If this happens, I would like to adjust the chosen height, so that lines are not cut off.如果发生这种情况,我想调整选择的高度,这样线条就不会被切断。 This is the code I have so far:
这是我到目前为止的代码:
double LineHeight = ??;
double requiredHeightAdjustment = this.Height % LineHeight;
if (requiredHeightAdjustment != 0)
{
this.Height -= requiredHeightAdjustment;
}
--Edit-- - 编辑 -
In case anyone needs this in the future here is what I ended up with:如果将来有人需要这个,这就是我最终得到的结果:
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;
}
This solution also makes the smallest necessary change to the chosen TextBox
size instead of always making a negative change.此解决方案还对所选的
TextBox
大小进行最小的必要更改,而不是总是进行负面更改。
Since your LineHeight is dependent on the font family and font size, you might want to look at the GlyphTypeface class in .NET 3.0+.由于您的 LineHeight 取决于字体系列和字体大小,您可能需要查看 .NET 3.0+ 中的GlyphTypeface class。 It might be a bit too low-level though, but it has properties like
Height
.虽然它可能有点太低级了,但它具有像
Height
这样的属性。
This has been asked several years ago, but just in case anyone lands here searching for a solution, you can use TextBlock.LineHeight
attached property to get TextBox
's LineHeight
:几年前就有人问过这个问题,但万一有人在这里寻找解决方案,您可以使用
TextBlock.LineHeight
附加属性来获取TextBox
的LineHeight
:
var LH = TextBlock.GetLineHeight(YOUR_TEXTBOX);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.