views:

2088

answers:

3

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>
A: 

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

17 of 26
I'm after the TextBlock filling ALL available space on top of the ellipse, using the DockPanel will divide the available area between the two.
TreeUK
Besides a DockPanel will not expand the size of the font to the maximum allowable in the available space even if the TextBlock's dimensions expand.I suspect it will be difficult to do...
Gordon Mackie JoanMiro
If you dock the ellipse to the top side of the DockPanel then it shouldn't change size. However, Joan's point about the text size not changing still stands.
17 of 26
+8  A: 

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:

http://stackoverflow.com/questions/729534/wpf-gui-that-changes-size-with-window/729743#729743

Rich
Turns out I was using it incorrectly :)
TreeUK
Good stuff Rich, saved my behind. +1 :)
Darko Z
A: 

Have you looked into transform ScaleTransform?

ib.

Ireney Berezniak
While it might be handy for resizing manually, I don't think it's the answer.
TreeUK