![](/img/trans.png)
[英]Wpf- How can I get the TreeViewItem that triggered the TreeViewItem.Loaded event?
[英]Wpf- How can I get the LineHeight of a normal TextBox in code?
我正在開發一個繼承自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 3.0+ 中的GlyphTypeface class。 雖然它可能有點太低級了,但它具有像Height
這樣的屬性。
幾年前就有人問過這個問題,但萬一有人在這里尋找解決方案,您可以使用TextBlock.LineHeight
附加屬性來獲取TextBox
的LineHeight
:
var LH = TextBlock.GetLineHeight(YOUR_TEXTBOX);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.