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.
我希望文本的大小是最大可能仍然适合控件的范围。在Windows编程中,我可以测量减小字体大小的文本大小,直到它适合目标维度。
Is there any way of doing this in Silverlight?
有没有办法在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.
答案是Rich描述使用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>
3 个解决方案
#1
21
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.
昨天还问了一个类似的问题,即相对于容器大小自动调整内容大小。在这种情况下的答案是相同的:使用Viewbox。如果将TextBlock放在Viewbox中,TextBlock将自行调整大小以仅使用它所需的空间,Viewbox将处理将其拉伸到容器的尺寸。使用stretch属性可从四种拉伸方法中选择一种。
Take a look at this thread from yesterday:
从昨天开始看看这个帖子:
WPF Gui that changes size with window?
用窗口改变大小的WPF Gui?
#2
0
Try a Dockpanel instead of a Grid. Using LastChildFill=true should get you the behavior that you're looking for.
尝试使用Dockpanel而不是网格。使用LastChildFill = true可以获得您正在寻找的行为。
#3
0
Have you looked into transform ScaleTransform?
你有没有研究过ScaleTransform?
ib.
IB。
#1
21
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.
昨天还问了一个类似的问题,即相对于容器大小自动调整内容大小。在这种情况下的答案是相同的:使用Viewbox。如果将TextBlock放在Viewbox中,TextBlock将自行调整大小以仅使用它所需的空间,Viewbox将处理将其拉伸到容器的尺寸。使用stretch属性可从四种拉伸方法中选择一种。
Take a look at this thread from yesterday:
从昨天开始看看这个帖子:
WPF Gui that changes size with window?
用窗口改变大小的WPF Gui?
#2
0
Try a Dockpanel instead of a Grid. Using LastChildFill=true should get you the behavior that you're looking for.
尝试使用Dockpanel而不是网格。使用LastChildFill = true可以获得您正在寻找的行为。
#3
0
Have you looked into transform ScaleTransform?
你有没有研究过ScaleTransform?
ib.
IB。