WPF version of .ScaleControl?

2019-07-21 09:59发布

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.

2条回答
我命由我不由天
2楼-- · 2019-07-21 10:42

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;
}
查看更多
在下西门庆
3楼-- · 2019-07-21 10:44

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

查看更多
登录 后发表回答