question

MERUNKUMARMAITY-4120 avatar image
1 Vote"
MERUNKUMARMAITY-4120 asked MERUNKUMARMAITY-4120 commented

How to scale font size in wpf?

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.

dotnet-csharpwindows-wpf
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

DaisyTian-1203 avatar image
1 Vote"
DaisyTian-1203 answered MERUNKUMARMAITY-4120 commented

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.

· 9
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for your answer sir. But this code is not working that means part of the code is correct but for this type of FontSize deceleration is not working. To use FontSize="{DynamicResource ControlFontSize}" we must declare it in App.xaml first. There are three type of window in my WPF application one is Mainwindow.xaml and another is for back end logic in C# and the last one is App.xaml where we have to declare the resource dictionary.

To use this type of "{DynamicResource ControlFontSize}" we must declare in App.xaml using the xaml language.

How I do that?

0 Votes 0 ·

@MERUNKUMARMAITY-4120
The code in my anwer work for me. and I don't create any ControlFontSize in the Resources. ControlFontSize is one of font sizes which program calculates FontSize according to the screen resolution.

0 Votes 0 ·

Thanks for your reply. I get this error : The resource "controlFontSize" could not be resolved

Sir, I suggest you to try this in your demo WPF application, and I sure that you definitely face this error.

If you have any other solution then please tell me. I am in trouble.

0 Votes 0 ·
Show more comments