Scale text in WPF - wpf

How can I automatically scale text inside a control based on the control's size but always make sure it's readable (that is, I want the font size stays between 9pt and 30pt)

The ViewBox can help with this, but I'm not sure about keeping the font size within a range. You can probably accomplish this roughly with MinHeight and MinWidth.

Try to use ScaleTransform.
In order to understand how to calculate the scale size you may refer to http://social.msdn.microsoft.com/forums/en-US/wpf/thread/d73c9eb2-9c68-428d-b99b-d23d0c62c095/

Related

How to properly use screen size in WPF application

I've done desktop application's GUI for minimum 800x600 pixels. But now I need to make it look good when maximised in any screen size. There is so little to show, I don't know how to adjust the elements in big screen.
Now in 800x600:
Maximised in 1366x768:
I tried to make them bigger with screen size, but that give them kinda ugly, unsophisticated look. What should I do?
You need to use WPF Grid Layout to position your control. It will stretch the size of your control based on the defined layout.
You can read a tutorial here https://www.wpftutorial.net/GridLayout.html
You can use Uniform grid in that case
http://www.c-sharpcorner.com/uploadfile/raj1979/uniformgrid-in-wpf/

How to set the PREFERRED size of a WPF control

Is there a way to set the preferred size of a WPF control without setting its maximum size?
I normally set all of my WPF controls to have have automatic height and width, and I set the alignment to stretch. That works great in a lot of cases. My windows come up in a reasonable size by default. If the user stretches the window, the extra space will go where I think it should.
This broke when I set the width of a DataGridTextColumn to "*". Now suddenly the preferred size for my table is several times bigger than my screen! The initial size for my window is exactly the size of my screen.
Of course, I could set the MaxWidth of my table (or my column), but I don't want to do that. The max width was fine at infinity.
I tried changing the Width property to a fixed number of pixels. That seems to change the maximum width. Even though I set HorizontalContentAlignment="Stretch" in the table, the table is centered and the width does not change.
Is there a way around this? I like auto-sizes most of the time. And I like setting the width of a column to "*". Is there a way to make them work together?
It seems like there's a property somewhere that I can't find. At some point the container has to ask my table what size it wants to be. That answer might be ignored or adjusted, but the table had to answer that question. It seems like I should be able to change that value, without changing the max size. (And it seems like if I say "stretch" and there is extra space, my table should be stretched.)
Thank you.

In wpf how to calculate pixel length of string?

I have a string to draw in a custom dialog box. How can i get the required length of string in pixels using WPF?
If you want to show it afterwards within a TextBlock, create the TextBlock and call Measure and Arrange. Make sure that the TextBlock has set the right font size before calling Measure.
Another way is to go via FormattedText, if you want to do your calculations on a low level.
You might not need the (pixel) size.
It might be better to automatically size the dialog to its content.

Shrinking Images rather than Stretching

I am having a little trouble in WPF - I am fully aware of the image stretch function and how to use it, and have that working fine. What I am trying to do however, is only apply the stretch if the image is too large (essentially to make it a shrink), and retain the original size if the image would fit into the view. The stretch method currently performs the shrinking perfectly, but will expand smaller images to fill the view which is not what I am after.
Is there a simple way of doing this?
you have to use the StretchDirection property in conjunction with the Stretch Property:
image.StretchDirection = StretchDirection.DownOnly;

How to make autosize text block in Expression Blend 3?

I'm trying to make a textblock control with a fixed size (height and width) in a window
using Expression Blend, but the content of the textblock will be changed programatically, so I want the text to be viewed by the largest possible font size without going outside the size of the rectangle.
How to do that?
Rather than changing the font size, use a ViewBox - WPF Tutorial. It seems to be better equipped for what you are trying to achieve as it gives the control within it the ability to resize indefinitely to the size of the area in which you place it.

Resources