问题
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