[英]UWP RichEditBox equivalent of GetCharIndexFromPosition
[英]UWP: Wrong computation of height for RichEditBox
我需要根據其內容計算RichEditBox的確切高度。 為此,我使用以下方法,在任何情況下都可以,但是在文本為一行時卻可以!
public static double GetElemHeight(FrameworkElement elem, double? actualWidth = null)
{
if (elem == null)
return 0;
// take note of the existing height, if any, since we have to re-establish it later:
double currentH = elem.Height;
if (!double.IsNaN(currentH))
elem.Height = double.NaN;
double totalW = (actualWidth ?? elem.Width) + elem.Margin.Left + elem.Margin.Right;
// Measure() only works as expected in this context if the Height is NaN:
elem.Measure(new Size(totalW, Double.PositiveInfinity));
Size size = elem.DesiredSize;
elem.Height = currentH; //re-establish the correct height
return size.Height - elem.Margin.Top - elem.Margin.Bottom;
}
基本上,對於RichEditBox中編寫的任何文本,該方法都會返回元素的正確高度。 但是,當我的文本僅覆蓋一行時,結果總是高度幾乎是正確結果的兩倍。
請在此處找到可重現該問題的MVC: https : //github.com/cghersi/UWPExamples/tree/master/SizeOfTextBox
關於我在做什么錯的任何線索嗎?
RichEditBox的默認高度為32px,這意味着當您的實際高度小於32時,它仍顯示32。在樣式上,控制內容的高度為Border,因此應更改Border的MinHeight。 另外,您可以轉到generic.xaml獲得RichEditBox的樣式。
<Page.Resources>
<Style TargetType="RichEditBox">
......
<Setter Property="SelectionFlyout" Value="{StaticResource TextControlCommandBarSelectionFlyout}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="RichEditBox">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<VisualStateManager.VisualStateGroups>
......
</VisualStateManager.VisualStateGroups>
......
<Border x:Name="BorderElement" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" CornerRadius="{TemplateBinding CornerRadius}" MinHeight="0" MinWidth="{ThemeResource TextControlThemeMinWidth}" Grid.RowSpan="1" Grid.Row="1"/>
......
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Page.Resources>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.