How to scale font size in wpf?

MERUN KUMAR MAITY 636 Reputation points
2021-05-06T06:40:42.32+00:00

Hi, there I use the popular Seoge UI font in my WPF application. And almost every position (like- Textbox,Grid,). I use the Fontsize =10 because in this size some letter are good looking. My problem is my application is DPI aware that means it changes it's size according to the screen resolution and DPI.

I do this using the DPI decorator class.

Here is the code of that class -

public class DpiDecorator : Decorator
    {
        public DpiDecorator()
        {
            this.Loaded += (s, e) =>
            {
                System.Windows.Media.Matrix m = PresentationSource.FromVisual(this).CompositionTarget.TransformToDevice;
                ScaleTransform dpiTransform = new ScaleTransform(1 / m.M11, 1 / m.M22);
                if (dpiTransform.CanFreeze)
                    dpiTransform.Freeze();
                this.LayoutTransform = dpiTransform;
            };
        }
    }

And after that I apply the DPI decorator in my Grid like this way -

<local:DpiDecorator>
<Grid>
</Grid>
</local:DpiDecorator>

As I say previously I use the Fontsize=10 because of good looking, I want to scale this font size into bigger and smaller according to it's parent controls that means in most of the cases I use the Text box to write anything on my UI.

My problem is If I run my application on another computer everything is OK that means the UI is automatically scaled and get bigger and smaller depending upon the DPI decorator and the fluent design of my UI. But the text on UI are looking blurry and small and looks like not properly increased it's size according to the UI.

How I solved this issue?

Though I found some solution on Stack overflow but that does not work.

Here is the link - https://stackoverflow.com/questions/15641473/how-to-automatically-scale-font-size-for-a-group-of-controls#:~:text=I%20have%20a%20few%20TextBlocks,the%20TextBlock%20into%20a%20ViewBox.&text=And%20it%20scales%20the%20font%20for%20each%20TextBlock%20automatically.

Developer technologies Windows Presentation Foundation
Developer technologies C#
0 comments No comments
{count} vote

Accepted answer
  1. DaisyTian-1203 11,646 Reputation points
    2021-05-06T07:53:20.043+00:00

    Please try below code to check if it work for your project:
    Step1: Add Loaded="Window_Loaded" for the Xaml page.
    Step2: Add code to it:

     private void Window_Loaded(object sender, RoutedEventArgs e)  
            {  
                double controlsize = ((SystemParameters.PrimaryScreenWidth / 12) / 3 * 2) / 5 * 0.7;  
                System.Windows.Application.Current.Resources.Remove("ControlFontSize");  
                System.Windows.Application.Current.Resources.Add("ControlFontSize", controlsize);  
            }  
    

    Step3 : In the Xaml page use FontSize="{DynamicResource ControlFontSize}" for the control like:

     <TextBlock Text="Button" HorizontalAlignment="Left" FontSize="{DynamicResource ControlFontSize}"  
                    Margin="239,163,0,0" VerticalAlignment="Top" Width="75"/>  
    

    If it doesn't work for your project, please let me know.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.