WPF version of .ScaleControl?

我的梦境 提交于 2020-01-04 06:01:22

问题


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

After (bad)

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)

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.


回答1:


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;
}



回答2:


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



来源:https://stackoverflow.com/questions/6403918/wpf-version-of-scalecontrol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!