可滚动文本块大小恰好为 2 行高

发布于 2024-11-06 06:02:03 字数 531 浏览 0 评论 0原文

我需要显示文本 - 最多 -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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

得不到的就毁灭 2024-11-13 06:02:03

只读无边框文本框可以作为文本块使用吗?

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

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

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

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

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" />

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.

柏林苍穹下 2024-11-13 06:02:03

一种更简单的解决方案(无需在值转换器中测量字符串并计算边距、填充等)是制作一个隐藏的(未折叠的)TextBlock,它只包含您想要的行数,并且将其 ActualHeight 绑定到可见 TextBlockMaxHeight。用作度量的不可见 TextBlock 必须将其 Visibility 设置为 Hidden,以便它仍然是布局计算的一部分,即使它不是显示(Collapsed 将导致它完全消失)。

这是一个演示,您可以将其放入新项目中以查看其工作原理(我使用 4 行来使滚动条更易于使用)。

这部分替换了 Window 内的默认 Grid

<Grid TextElement.FontSize="12">
    <TextBlock Name="limiter"
               HorizontalAlignment="Left"
               VerticalAlignment="Top"
               Width="100"
               Visibility="Hidden"
               Background="Gray">
    </TextBlock>
    <ScrollViewer MaxHeight="{Binding ElementName=limiter, Path=ActualHeight}"
                  Margin="40,0"
                  VerticalAlignment="Top"
                  HorizontalAlignment="Stretch"
                  Background="LightGray"
                  VerticalScrollBarVisibility="Auto">
        <TextBlock Name="vis" />
    </ScrollViewer>
    <Button Name="AddLine"
            Padding="8"
            VerticalAlignment="Bottom"
            HorizontalAlignment="Center"
            Click="AddLine_Click">Add A Line</Button>
</Grid>

Window 的构造函数中,我将所需的行数添加到隐藏的 >TextBlock(您还可以在 XAML 中使用 RunLineBreak 元素):

var limitString = "AgjZ";
limitString = limitString + Environment.NewLine + limitString + Environment.NewLine + limitString + Environment.NewLine + limitString;
limiter.Text = limitString;

ButtonAddLine_Click< /code> handler 看起来像这样,所以你可以看到效果不同的行数:

private int counter = 0;
private void AddLine_Click( object sender, RoutedEventArgs e ) {
    var newline = string.Empty;
    if ( !string.IsNullOrWhiteSpace( vis.Text ) )
        newline = Environment.NewLine;
    vis.Text += string.Format( newline + "This is line #{0}.", ++counter );
}

请记住,隐藏的 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 its ActualHeight to the visible TextBlock's MaxHeight. The invisible TextBlock used as the measure must have its Visibility set to Hidden 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 the Window:

<Grid TextElement.FontSize="12">
    <TextBlock Name="limiter"
               HorizontalAlignment="Left"
               VerticalAlignment="Top"
               Width="100"
               Visibility="Hidden"
               Background="Gray">
    </TextBlock>
    <ScrollViewer MaxHeight="{Binding ElementName=limiter, Path=ActualHeight}"
                  Margin="40,0"
                  VerticalAlignment="Top"
                  HorizontalAlignment="Stretch"
                  Background="LightGray"
                  VerticalScrollBarVisibility="Auto">
        <TextBlock Name="vis" />
    </ScrollViewer>
    <Button Name="AddLine"
            Padding="8"
            VerticalAlignment="Bottom"
            HorizontalAlignment="Center"
            Click="AddLine_Click">Add A Line</Button>
</Grid>

In the Window's constructor I add the however many lines I want to the hidden TextBlock (you could also use Run and LineBreak elements in the XAML):

var limitString = "AgjZ";
limitString = limitString + Environment.NewLine + limitString + Environment.NewLine + limitString + Environment.NewLine + limitString;
limiter.Text = limitString;

The Button's AddLine_Click handler looks like this, so you can see the effect with different numbers of lines:

private int counter = 0;
private void AddLine_Click( object sender, RoutedEventArgs e ) {
    var newline = string.Empty;
    if ( !string.IsNullOrWhiteSpace( vis.Text ) )
        newline = Environment.NewLine;
    vis.Text += string.Format( newline + "This is line #{0}.", ++counter );
}

Remember that the hidden TextBlock needs to be in pretty much the same scope as the one you want to show so that it inherits FontSize and other font-related values. If anything is set on the ScrollViewer or visible TextBlock, it needs to go on the hidden TextBlock, too. You could also use binding to make sure the hidden one has the same style as the visible one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文