如何让 Silverlight 调整文本内容大小以适应?

发布于 2024-07-17 06:27:24 字数 1054 浏览 4 评论 0原文

我有一个用户控件(如下),我将文本绑定到数据源并实例化一堆用户控件。

我希望文本的大小尽可能最大,并且仍然适合控件的边界。 在 Windows 编程中,我可以通过减小字体大小来测量文本大小,直到它适合目标尺寸。

在 Silverlight 中有什么方法可以做到这一点吗?

我知道我大概可以以类似的方式做到这一点,但是有没有任何“更好”的方法来做到这一点?

<Grid x:Name="gdBubble" Width="180" Height="95">
    <Ellipse x:Name="elBubble" Fill="#FFFFA300" />
    <TextBlock x:Name="txtContent" Text="{ Binding ClientName }" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>

[我在这里使用网格是为了使文本块正确居中。]

答案正如 Rich 所描述的那样使用 Viewbox。

这是获胜的配置(对我来说):

<Grid x:Name="gdBubble" Width="180" Height="95">
    <Ellipse x:Name="elBubble" Fill="#FFFFA300" />
    <controls:Viewbox Margin="10,10,10,10" VerticalAlignment="Stretch" Height="Auto">
        <TextBlock x:Name="txtContent" FontSize="18" Text="{ Binding ClientName }" HorizontalAlignment="Center" VerticalAlignment="Center" />
    </controls:Viewbox>
</Grid>

I've got a user control (below), I'm binding the text to a datasource and instancing up a bunch of the usercontrols.

I want the size of the text to be the largest possible that will still fit in the bounds of the control. In Windows programming, I could measure the text size decrementing the font size until it fit the target dimensions.

Is there any way of doing this in Silverlight?

I know I could presumably do it in a similar way, but are there any 'nicer' ways of doing it?

<Grid x:Name="gdBubble" Width="180" Height="95">
    <Ellipse x:Name="elBubble" Fill="#FFFFA300" />
    <TextBlock x:Name="txtContent" Text="{ Binding ClientName }" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>

[I'm using a grid here in order for the textblock to center correctly.]

The answer was as Rich described to use a Viewbox.

This was the winning configuration (for me):

<Grid x:Name="gdBubble" Width="180" Height="95">
    <Ellipse x:Name="elBubble" Fill="#FFFFA300" />
    <controls:Viewbox Margin="10,10,10,10" VerticalAlignment="Stretch" Height="Auto">
        <TextBlock x:Name="txtContent" FontSize="18" Text="{ Binding ClientName }" HorizontalAlignment="Center" VerticalAlignment="Center" />
    </controls:Viewbox>
</Grid>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

带上头具痛哭 2024-07-24 06:27:24

昨天有人提出了一个类似的问题,关于相对于容器的大小自动调整内容的大小。 在这种情况下,答案是相同的:使用 Viewbox。 如果将 TextBlock 放入 Viewbox 中,TextBlock 将调整自身大小以仅使用其所需的空间,并且 Viewbox 将处理将其拉伸到容器的尺寸。 使用拉伸属性可以从四种拉伸方法中选择一种。

看一下昨天的这个帖子:

WPF Gui 改变大小有窗户吗?

A similar question was asked yesterday about resizing content automatically relative to the size of a container. The answer in this case is the same: use a Viewbox. If you put your TextBlock inside of the Viewbox, the TextBlock will resize itself to only use the space it needs, and the Viewbox will handle stretching this to the dimensions of the container. Use the stretch attribute to choose from one of four stretching methods.

Take a look at this thread from yesterday:

WPF Gui that changes size with window?

素年丶 2024-07-24 06:27:24

尝试使用 Dockpanel 而不是网格。 使用 LastChildFill=true 应该可以获得您正在寻找的行为。

Try a Dockpanel instead of a Grid. Using LastChildFill=true should get you the behavior that you're looking for.

初熏 2024-07-24 06:27:24

您研究过变换 ScaleTransform 吗?

ib.

Have you looked into transform ScaleTransform?

ib.

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