WPF version of .ScaleControl?
Asked Answered
P

2

2

What is the WPF version of Control.ScaleControl?


i am trying to honor the user's font preference by setting the font to the IconTitleFont:

private void ApplyUserFontPreferences()
{
   this.FontFamily = SystemFonts.IconFontFamily;
   this.FontSize = SystemFonts.IconFontSize;
   this.FontStyle = SystemFonts.IconFontStyle;
   this.FontWeight = SystemFonts.IconFontWeight;
}

Unlike WinForms, the contents of the form are not scaled with the font change:

Before
enter image description here

After (bad)
enter image description here

In reality all controls on the form (including the size of buttons, the width of listview columns, etc) should scale to match the new layout:

After (good)
enter image description here

Since WPF doesn't (unlike WinForms) respond to font sizes changes, i was going to workaround the issue by trying to scale the WPF form myself, using a hypothetical WPF version of ScaleControl:

private void ApplyUserFontPreferences()
{
   Double scaleFactor = (SystemFonts.IconFontSize / this.FontSize); //i.e. new / old
   this.ScaleControl(scaleFactor); //doesn't exist

   this.FontFamily = SystemFonts.IconFontFamily;
// this.FontSize = SystemFonts.IconFontSize;
   this.FontStyle = SystemFonts.IconFontStyle;
   this.FontWeight = SystemFonts.IconFontWeight;
}

Another example of wanting to scale a control (and all child controls) is when i need to scale a control (and all child controls) to fit in a given size. In this case i don't want to scale an entire form, i only want to scale a particular control.

Peculiarity answered 19/6, 2011 at 17:21 Comment(2)
I can't understand what's the question?Bonanno
@Navid Rahmani: What is the WPF method used to scale a control (and all child controls)?Peculiarity
B
2

What about this solution

<Window
   x:Class="WpfApplication1.MainWindow"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   FontSize="40"
   Loaded="Window_Loaded"
   SizeToContent="WidthAndHeight"
   Title="MainWindow">

   <Grid x:Name="LayoutRoot" Width="525" Height="350">
      <Button Width="300" Height="60" Content="Hello world"/>
      <Grid.LayoutTransform>
         <ScaleTransform x:Name="scaleTransform"/>
      </Grid.LayoutTransform>
   </Grid>
</Window>

And in the code behind

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    ApplyUserFontPreferences();
}

private void ApplyUserFontPreferences(){ 
    Double scaleFactor = (SystemFonts.IconFontSize / this.FontSize);

    this.scaleTransform.ScaleX = scaleFactor;
    this.scaleTransform.ScaleY = scaleFactor;       

    this.FontFamily = SystemFonts.IconFontFamily; 
    this.FontStyle = SystemFonts.IconFontStyle;
    this.FontWeight = SystemFonts.IconFontWeight;
}
Bonanno answered 20/6, 2011 at 0:32 Comment(1)
i wouldn't want elements to be as large as their content. e.g. a listview column can be 120px wide. If the font of the listview gets 11% larger, then the column should then be 133px wide (120px * 111% = 133.2px)Peculiarity
D
0

I'm not sure if it matches exactly what you are looking for, but WPF does include an automatic scaling control: Viewbox.

It's a bit heavy-handed, so YMMV. Eventually, you'll probably find that you want more precise control, so you'll have to instead take care in designing your templates and so forth. However, Viewbox will get you some basic scaling functionality.

See also: Resolution Independance in WPF

Dispersal answered 20/6, 2011 at 17:28 Comment(5)
How do you mean heavy handed? What are some of it's caveats? The description (Defines a content decorator that can stretch and scale a single child to fill the available space.) sounds right up my alley.Peculiarity
@Ian - take a look at the link I've added to my answer. There are some performance hits if used too much (you'll have to Google it - I don't have a source offhand). That being said, even if you only use it a little bit, you may find that eventually you want better control over smaller areas (sometimes scaled fonts don't look quite right in the context of your UI, or if you use different Viewboxes, you get different sized fonts for the same control on the same UI, etc.). Best you can do is to try it out and see if it, well, fits what you are asking it to.Dispersal
(i.e.: if it works, great! if not, you have some more work to do.) :)Dispersal
@Wonko the Sane: Don't look at me! i didn't upvote, or downvote, until i get a chance to try it later tonight. Although technically informing me about the existance of the Viewbox is "useful".Peculiarity
Oh, I wasn't trying to blame ya... :) I can accept a downvote (otherwise I wouldn't be a member). However, I don't understand downvoting a question without explanation - there is no opportunity to improve the answer.Dispersal

© 2022 - 2024 — McMap. All rights reserved.