我正在开发一个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大小进行最小的必要更改,而不是始终进行负面更改。