簡體   English   中英

Wpf-如何在代碼中獲取普通 TextBox 的 LineHeight?

[英]Wpf- How can I get the LineHeight of a normal TextBox in code?

我正在開發一個繼承自TextBoxCustomControl ,可以通過在拖動鼠標時按住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 3.0+ 中的GlyphTypeface class。 雖然它可能有點太低級了,但它具有像Height這樣的屬性。

幾年前就有人問過這個問題,但萬一有人在這里尋找解決方案,您可以使用TextBlock.LineHeight附加屬性來獲取TextBoxLineHeight

var LH = TextBlock.GetLineHeight(YOUR_TEXTBOX);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM