可滚动文本块大小正好 2 行高 [英] Scrollable TextBlock Sized EXACTLY 2 Lines High

查看:23
本文介绍了可滚动文本块大小正好 2 行高的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要显示文本 - 最多 -2 行 - 没有可见的垂直滚动,然后在大于 2 行时显示滚动:

I need the text to display - at most -2 lines - with no vert scroll visible, then show the scroll when greater than 2 lines:

<Border BorderBrush="Black" BorderThickness="1" Grid.Row="1" Grid.ColumnSpan="2">
    <ScrollViewer VerticalScrollBarVisibility="Auto" MaxHeight="40">
        <TextBlock Text="{Binding RoadsString}" Style="{StaticResource WrapTextStyle}"/>
    </ScrollViewer>
</Border>

我可以试验 MaxHeight 值,直到它看起来正确 - 但我更喜欢它适应指定的任何字体并且......精确.最好使用 XAML.

I can experiment with the MaxHeight value until it's looks right - but I'd prefer that it accommodate whatever font is specified and ... precise. Preferably in XAML.

我可以将 MaxHeight 绑定到字体大小吗?或者我还没有找到 TextBlock 属性.

Can I bind MaxHeight to the font size? Or perhaps there a TextBlock property I haven't found.

谢谢,丹

推荐答案

只读无边框 TextBox 是否适合作为 TextBlock 使用?

Would a readonly borderless TextBox work for you as a TextBlock?

<TextBox MinLines="2" MaxLines="2" IsReadOnly="True" BorderThickness="0"
VerticalScrollBarVisibility="Auto" Width="200" VerticalAlignment="Top" />

MinLinesMaxLines 将限制文本框始终只有两个行(除非您显式设置了 Height - 如果在 TextBox 上显式设置了 Height 属性,则 MaxLines 和 MinLines 属性值将被忽略).

MinLines and MaxLines would restrict textbox to always have exactly two lines (except if you set Height explicitly - If the Height property is explicitly set on a TextBox, the MaxLines and MinLines property values are ignored).

我发现这种方法的一个问题是 TextBox 的文本不会与其他文本块左对齐(如果您将它们放在网格的同一列中).

One problem I see with this approach is that the TextBox's text won't be left aligned with other textblocks (if you have placed them in same column of a grid).

刚刚注意到您实际上想要文本块的边框.为此,您可以去掉 BorderThickness="0" 部分.

Just noticed that you actually want a border for your textblock. For that you can get rid of BorderThickness="0" part.

这篇关于可滚动文本块大小正好 2 行高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆