2

什么是 WPF 版本Control.ScaleControl


我试图通过将字体设置为IconTitleFont来尊重用户的字体偏好:

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

与 WinForms 不同,表单的内容不会随着字体的变化而缩放:


在此处输入图像描述

之后(坏)
在此处输入图像描述

实际上,表单上的所有控件(包括按钮的大小、列表视图列的宽度等)都应该缩放以匹配新布局:

之后(好)
在此处输入图像描述

由于 WPF 不(与 WinForms 不同)响应字体大小的变化,我打算通过尝试自己缩放 WPF 表单来解决这个问题,使用假设的 WPF 版本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;
}

想要缩放控件(和所有子控件)的另一个示例是当我需要缩放控件(和所有子控件)以适应给定大小时。在这种情况下,我不想缩放整个表单,我只想缩放特定控件。

4

2 回答 2

2

这个解决方案怎么样

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

在后面的代码中

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;
}
于 2011-06-20T00:32:05.743 回答
0

我不确定它是否完全符合您的要求,但 WPF 确实包含一个自动缩放控件:Viewbox

这有点笨拙,所以YMMV。最终,您可能会发现您需要更精确的控制,因此您必须在设计模板等时小心谨慎。但是,Viewbox 将为您提供一些基本的缩放功能。

另请参阅:WPF 中的分辨率独立性

于 2011-06-20T17:28:33.620 回答