首页 文章

WPF字体缩放

提问于
浏览
8

我有一个WPF应用程序,其中应缩放用户界面,以便在窗口变大时它应该变大 . 在其中一个对话框中,我需要向用户显示项目列表,用户应单击其中一个 . 该列表将包含1到15-20个项目 . 我希望每个项目的字体大小与列表中其他项目的字体大小一样大,但同时我希望在窗口变大时字体大小增加 .

目前,我的测试代码如下所示 .

<Window x:Class="WpfApplication4.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:controls="clr-namespace:WpfApplication4"
    Title="Window1" Height="480" Width="640">


    <ScrollViewer>

        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="30*" MinHeight="30"/>
                <RowDefinition Height="30*" MinHeight="30"/>
                <RowDefinition Height="30*" MinHeight="30"/>
            </Grid.RowDefinitions>

            <Button Grid.Row="0" MaxHeight="100"><Viewbox><TextBlock>T</TextBlock></Viewbox></Button>
            <Button Grid.Row="1" MaxHeight="100"><Viewbox><TextBlock>Test</TextBlock></Viewbox></Button>
            <Button Grid.Row="2" MaxHeight="100"><Viewbox><TextBlock>Test Longer String</TextBlock></Viewbox></Button>

        </Grid>

    </ScrollViewer>

</Window>

如果应用程序启动并且窗口变宽,则一切看起来都正常 . 如果窗口宽度减小,文本 Test Longer String 的字体大小会变小,但 TTest 的字体大小保持不变 . 我明白为什么会发生这种情况 - 视图框会将内容缩放到最大尺寸 . 我想知道的是我应该用什么方法来解决这个问题 .

我不想给控件特定的字体大小,因为有些人会在640x480这样的低分辨率屏幕上运行它,而其他人会使用更大的宽屏 .

EDIT:

我试图将我的代码修改为以下内容:

<ScrollViewer>
    <Viewbox>
        <ItemsControl>
            <Button>Test 2</Button>
            <Button>Test 3</Button>
            <Button>Test 4 afdsfdsa fds afdsaf</Button>
            <Button>Test 5</Button>
            <Button>Test 5</Button>
            <Button>Test 5</Button>
            <Button>Test 5</Button>
            <Button>Test 5</Button>
        </ItemsControl>
    </Viewbox>
</ScrollViewer>

但随着按钮边框的大小也增加,因此在大屏幕上,按钮边框变为厘米宽 .

4 回答

  • -1

    试试这个:渲染你的文本项,就像你在任何时候一样:

    • TextBlock s包含在_1698439中?

    • ListBox

    将整个shebang放入 ViewBox 并将其设置为适合您需要的比例 . 查找水平,垂直或组合缩放属性 .

  • 0

    此解决方法可能有所帮助:

    <ScrollViewer>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="30*" MinHeight="30"/>
                <RowDefinition Height="30*" MinHeight="30"/>
                <RowDefinition Height="30*" MinHeight="30"/>
            </Grid.RowDefinitions>
    
            <Button Grid.Row="0" MaxHeight="100">
                <Viewbox>
                    <TextBlock Width="{Binding ElementName=tbLonger,Path=ActualWidth}">T</TextBlock>
                </Viewbox>
            </Button>
            <Button Grid.Row="1" MaxHeight="100">
                <Viewbox>
                    <TextBlock Width="{Binding ElementName=tbLonger,Path=ActualWidth}">Test</TextBlock>
                </Viewbox>
            </Button>
            <Button Grid.Row="2" MaxHeight="100">
                <Viewbox>
                    <TextBlock Name="tbLonger">Test Longer String</TextBlock>
                </Viewbox>
            </Button>
        </Grid>
    </ScrollViewer>
    

    关键是要将所有文本块设置为相同的宽度 . 在这种情况下,它们都通过绑定遵循最长的文本块 .

  • 0

    我必须制作一个独立于分辨率的应用程序,并在此处使用此答案:tips on developing resolution independent application

    您的应用程序正在缩放(或缩放),具体取决于分辨率 .

  • 0

    最简单的方法可能是构建一个装饰器来完成这项工作 . 你可以称之为“VerticalStretchDecorator”或类似的东西 .

    以下是它的使用方法:

    <UniformGrid Rows="3" MaxHeight="300">
      <Button>
        <my:VerticalStretchDecorator>
          <TextBlock>T</TextBlock>
        </my:VerticalStretchDecorator>
      </Button> 
      <Button>
        <my:VerticalStretchDecorator>
          <TextBlock>Test</TextBlock>
        </my:VerticalStretchDecorator>
      </Button> 
      <Button>
        <my:VerticalStretchDecorator>
          <TextBlock>Test Longer String</TextBlock>
        </my:VerticalStretchDecorator>
      </Button>
    </UniformGrid>
    

    我使用 UniformGrid 而不是 Grid ,但它与 Grid 的工作方式相同 .

    它将实现如下:

    public class VerticalStretchDecorator : Decorator
    {
      protected override Size MeasureOverride(Size constraint)
      {
        var desired = base.Measure(constraint);
        if(desired.Height > constraint.Height || desired.Height==0)
          LayoutTransform = null;
        else
        {
          var scale = constraint.Height / desired.Height;
          LayoutTransform = new ScaleTransform(scale, scale); // Stretch in both directions
        }
      }
    }
    

相关问题