可滚动文本块大小恰好为 2 行高
我需要显示文本 - 最多 -2 行 - 没有可见的垂直滚动,然后在大于 2 行时显示滚动:
<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 中。
我可以将 MaxHeight 绑定到字体大小吗?或者也许有一个我还没有找到的 TextBlock 属性。
谢谢, 担
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>
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.
Can I bind MaxHeight to the font size? Or perhaps there a TextBlock property I haven't found.
Thanks,
Dan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只读无边框文本框可以作为文本块使用吗?
MinLines 和 MaxLines 将限制文本框始终只有两行(除非显式设置 Height - 如果在 TextBox 上显式设置 Height 属性,则忽略 MaxLines 和 MinLines 属性值)。
我发现这种方法的一个问题是文本框的文本不会与其他文本块左对齐(如果您将它们放置在网格的同一列中)。
编辑:刚刚注意到您实际上想要文本块的边框。为此,您可以去掉 BorderThickness="0" 部分。
Would a readonly borderless TextBox work for you as a TextBlock?
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).
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).
EDIT: Just noticed that you actually want a border for your textblock. For that you can get rid of BorderThickness="0" part.
一种更简单的解决方案(无需在值转换器中测量字符串并计算边距、填充等)是制作一个隐藏的(未折叠的)
TextBlock
,它只包含您想要的行数,并且将其ActualHeight
绑定到可见TextBlock
的MaxHeight
。用作度量的不可见TextBlock
必须将其Visibility
设置为Hidden
,以便它仍然是布局计算的一部分,即使它不是显示(Collapsed
将导致它完全消失)。这是一个演示,您可以将其放入新项目中以查看其工作原理(我使用 4 行来使滚动条更易于使用)。
这部分替换了
Window
内的默认Grid
:在
Window
的构造函数中,我将所需的行数添加到隐藏的>TextBlock
(您还可以在 XAML 中使用Run
和LineBreak
元素):Button
的AddLine_Click< /code> handler 看起来像这样,所以你可以看到效果不同的行数:
请记住,隐藏的
TextBlock
需要与您要显示的范围几乎相同,以便它继承FontSize
和其他与字体相关的内容价值观。如果在ScrollViewer
或可见的TextBlock
上设置了任何内容,它也需要在隐藏的TextBlock
上进行设置。您还可以使用绑定来确保隐藏的样式与可见的样式相同。One of the simpler solutions (without getting into measuring strings and calculating margins, padding, etc. in a value converter) is to make a hidden (not collapsed)
TextBlock
that only has however many lines you want and bind itsActualHeight
to the visibleTextBlock
'sMaxHeight
. The invisibleTextBlock
used as the measure must have itsVisibility
set toHidden
so that it is still part of layout calculations even if it isn't shown (Collapsed
will cause it to disappear altogether).Here's a demonstration you can throw into a new project to see it work (I use 4 lines to make the scrollbar easier to use).
This part replaces the default
Grid
inside theWindow
:In the
Window
's constructor I add the however many lines I want to the hiddenTextBlock
(you could also useRun
andLineBreak
elements in the XAML):The
Button
'sAddLine_Click
handler looks like this, so you can see the effect with different numbers of lines:Remember that the hidden
TextBlock
needs to be in pretty much the same scope as the one you want to show so that it inheritsFontSize
and other font-related values. If anything is set on theScrollViewer
or visibleTextBlock
, it needs to go on the hiddenTextBlock
, too. You could also use binding to make sure the hidden one has the same style as the visible one.