1

Possible Duplicate:
WPF 4: Grow/Shrink TextBlock (Font Size) based on available space

I ma looking for a following behaviours in WPF: All the TextBlock in my app are shown in the 24 Font size, but if there isn't enough space I would to decrease a font size.

I thought of two solutions, but none of them work:

  1. Put a textBox inside viewbox. Doesn't work, as all labels start to get different sizes.
  2. If that would be a button I would specify a MinWidth instead of Width, but there is no property called MinFontSize on the TextBlock. Edit:
  3. Create a custom control with property MinTextWidth (this might work)

Any other solutions, which would work?

Community
  • 1
  • 1
Vitalij
  • 4,587
  • 9
  • 42
  • 65
  • Would it work to put the whole bunch of text blocks in one big viewbox? – David Yaw Feb 24 '11 at 16:07
  • I would suggest that rather than reducing the font size follow the lead of others and shorten the strings to fit using ellipsis. – Lazarus Feb 24 '11 at 16:12
  • Hmm, I think it won't as it will just ensure that all elements are scaled rather than individual element change it's size – Vitalij Feb 24 '11 at 16:14
  • The data in the textBox is numerical and crucial to the user, and there is no space to wrap that text and it must be shown. But it is a good solution, I have used it in places where I show a lot of text, but in this case I can't use it. – Vitalij Feb 24 '11 at 16:16

2 Answers2

1

I would say this is a design issue, if the user can't see the entire item, shrinking the text will probably not help as it may make it unreadable.

You should think about doing something where either the TextBlock grows to fit the text, or your use an expander or something to allow them to see the rest of the text.

msarchet
  • 15,104
  • 2
  • 43
  • 66
  • Yes if it would be a text rather than numerical value. The text is numerical and is displayed on the gauge and in normal situations should fit in the available space, however in some situation where the input is outside the range, It is a must to show it and decreasing the font size is better than trimming the text. – Vitalij Feb 26 '11 at 12:46
0

I think there isn't generic solution for this issue. There is one simple way: layout root for your window is ViewBox. Another way, by my opinion, is to write custom control and dynamically change ScaleTransform (not FontSize - this might causes perfomance issues).

Y.Yanavichus
  • 2,387
  • 1
  • 21
  • 34
  • The problem with putting it all into a ViewBox is that all content will be scaled equally, rather than having individual textbox decrease it's size, when there isn't enough space. – Vitalij Feb 24 '11 at 21:07
  • Making a custom control is an option, however if you change the ScaleTransform rather than font size it won't affect the real size of the textbox. – Vitalij Feb 24 '11 at 21:30
  • 1
    @Vitalij: Ok, but change font size by step, for instance 12,13,14,15,... Do not use calculated double value, in this case process PresentationFontCache may consume a lot of memory. – Y.Yanavichus Feb 25 '11 at 03:07
  • Yea, you are right, it will just have loads of cached fonts rather than few, if I use double instead of ints for font sizes. – Vitalij Feb 25 '11 at 09:02
  • @Vitalij: Did you fing any solution? – Y.Yanavichus Mar 14 '11 at 04:14