How to change the label font size dynamically? - wpf

I wrote some wpf application that contain labels.
I want to change the label font size according to the screen .. that mean that if the user will maximize the application window - the font will grow up - if he will change the size to small window - the font will shrink.
I try a lot of solutions - but the font size is not changing.
need help ...
Thanks.

Try using a ViewBox
<Grid>
<Viewbox>
<Label >Hello world</Label>
</Viewbox>
</Grid>

Using FormattedText class, you can find out Width and Height of given text with specified Fontfamily. You can use this method to calculate and find out how much font size is needed to fill up available space.

Related

Change font size in WPF compatible with Ease of Access functions

I am working on a simple WPF app that needs to be accessible for people with impaired vision.
I am testing my app with the Windows 10 -> Settings -> Ease of Access -> Display -> Make text Bigger slider setting.
I have some section titles (TextBlock) that I want to have a bigger font than the rest of the labels and text in the window. However, if I use the FontSize attribute to make it bigger it is no longer affected by changes to the system text size. Thus, while all other text that hasn't FontSize set gets bigger, the ones that do remain at their fixed size.
Is there a simple way to make some text relatively bigger than the rest while not setting a specific font size value?
Is there a simple way to make some text relatively bigger than the rest while not setting a specific font size value?
I am afraid there is no support for relative font sizes in WPF but you could try to apply a ScaleTransform:
<TextBlock Text="Bigger">
<TextBlock.RenderTransform>
<ScaleTransform ScaleX="1.2" ScaleY="1.2" />
</TextBlock.RenderTransform>
</TextBlock>
This achieved exactly what I wanted. It is not the prettiest, but it works well.
https://github.com/dotnet/wpf/issues/3701

ViewBox automatically change postion of my control

I am writing an application using WPF in visual studio 2012.
I want to use Viewbox control to solve problem of resizing application. Now when I try to place other controls in Viewbox, I see that I can't change the position of controls. It seems that Viewbox change automatically the positions of controls!
Picture(1) is result
and picture(2) is what I want.
picture(1)
picture(2)
Please help me.
You need to specify how you want the ViewBox to align the child element it's stretching.
<Viewbox VerticalAlignment="Top">
Should place your elements at your desired location.
You can also specify how the ViewBox will Stretch it's content, and the HorizontalAlignment, if required.
Additionaly, you can specify StretchDirection="UpOnly", to ensure that your controls are never scaled below the initial size you specify.

resolution in WPF

In my applcaiton,i make Grid of size Grid Width="1300.441" Height="80",
Its working fien in this resolution.But when i changed my resolution to 1152 width,quater of application get out of the desktop.
How can i avoid that?
I want to display full page in all resolution.
Pls help me
In WPF, the pixel size is 1/96 inch. There's no other resolution. It seems that your content is 13.54626 inches wide. My guess is that this is an XPS document or a raster or something? In this case, there is no way to display the whole thing in 1152 pixels at 100% without cropping and needing to scroll.
You can place your grid inside a ViewBox element, it will scale everything down to whatever resolution you need.
<ViewBox Width="1000">
<Grid Width="1300.441">
...
</Grid>
</ViewBox>

Can I limit WPF TextBlock height to two lines?

I just received a requirement to display a length of text in a control. The control is of a particular width and will be up to 2 lines in height. If it renders longer than two lines it will just display "..." at the end of the string.
Is this possible with any of the stock standard WPF controls?
Thanks,
D.
Set the Height of the TextBlock to be high enough to fit two lines. Set the TextWrapping to Wrap and the TextTrimming to CharacterEllipsis or WordEllipsis.
For the default Segoe UI 12Pt font, I find this does it
<TextBlock TextWrapping="Wrap" Height="40" TextTrimming="CharacterEllipsis" />
You could probably do some code behind to work out the height it should be be for a particular font if you want.
That's not standard behavior that I've ever found, but again I've not looked for it.
One possibility is to use a monospace font in a TextArea control, and then if the string is greater than however many characters fit in the area, only display the right N characters with the ellipses

In WPF, What is the best way to create toolbar buttons so that the images are properly scaled?

Specifically, I'm looking to use the 16*16 32-bit png images included with the VS2008ImageLibrary. I've tried manually setting the Height and Width attributes of the image, adjusting margins and padding, adjusting Stretch and RenderOptions. My attempts to create toolbar buttons have all led to either Improper Scaling (blurry icons), the bottom row of pixels on the icon being truncated, or the toolbar button being improperly sized - not to mention the disappearing icons already mentioned Here. Has anyone found the best way to make standard, VisualStudio/WinForms-style toolbar buttons that display properly in WPF?
First, change the image resolution to 96DPI, this can done with the free Paint.net ( http://www.getpaint.net ) by opening the file, Selecting Image->Canvas Size from the menu and adjusting the "resolution" to 96 and saving.
If this doesn't help you can then use the solution I wrote about in my blog here http://www.nbdtech.com/blog/archive/2008/11/20/blurred-images-in-wpf.aspx
Best way would be using Vector graphics instead of png. I know the following is not exactly what you asked for, but imho there is no way for better looking icons. Also it would help you get rid off margins and paddings. (Ok, if you want to use photos you're screwed)
Bad News is you probably need to repaint all your icons. You could do this using MS Expression Blend(it's capable to save painted Images as .xaml) or you make them on our own with a texteditor.
I prefer the Border.Background instead of the Image.Source for placing the icon, this allows me to put text over the image. This would look samething like that:
<Window.Resources>
<ResourceDictionary Source="Resources/Icons.xaml"/>
</Window.Resources>
<!--
...
-->
<Button>
<Border Background="{StaticResource IconName}" Height="16" Width="16" />
</Button>
The best workaround I can come up with is this:
<Image x:Key="TB_NewIcon" Source="Toolbar Images/NewDocumentHS.png" Height="16" Width="16" SnapsToDevicePixels="True" RenderOptions.BitmapScalingMode="NearestNeighbor"/>
...
<Button Command="ApplicationCommands.New" Content="{StaticResource TB_NewIcon}" Padding="2,2,2,1"/>
Or Alternatively:
<BitmapImage x:Key="TB_NewIcon" UriSource="Toolbar Images\NewDocumentHS.png"/>
...
<Button Command="ApplicationCommands.New" Padding="2,2,2,1">
<Image Source="{StaticResource TB_NewIcon}" Height="16" Width="16" SnapsToDevicePixels="True" RenderOptions.BitmapScalingMode="NearestNeighbor"/>
</Button>
For the Button Tag, the Padding attribute is needed to ensure that the image isn't truncated at a height of 15 pixels, and that the button isn't resized to fit the image. Alternatively, we could specify Padding="1", but then we must manually set Height="21" and Width="22" to ensure the button isn't resized to fit the image
On the Image Tab, the Height and Width are needed to ensure that the image isn't stretched. SnapsToDevicePixels and RenderOptions.BitMapScalingMode are both needed to ensure that there is no blurring. I can't promise that this will work nicely for all resolutions.
Note:
For the NewDocumentHS.png icon, the one that causes the most issues,as it takes up the full 16 pixels of height, you may want to adjust the padding to "1,1,3,2", so that the bottom aligns more properly with other icons.
You may want to consider trying a new property available now in WPF4.
Leave the RenderOptions.BitmapScalingMode to HighQuality or just don't declare it.
On your root element (i.e. your main window) add this property: UseLayoutRounding="True".
A property previously only available in Silverlight has now fixed all Bitmap sizing woes. :)
Please Note - a few of the effects layout rounding
can have on exact layout:
width and or height of elements may grow or shrink by at most 1 pixel
placement of an object can move by at most 1 pixel
centered elements can be vertically or horizontally off center by at most
1 pixel
More info found here: http://blogs.msdn.com/text/archive/2009/08/27/layout-rounding.aspx

Resources