WPF 字体缩放

标签 wpf user-interface scaling font-size

我有一个 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保持原样。我确实理解为什么会发生这种情况 - View 框会将内容缩放到其最大大小。我想知道的是我应该用什么方法来解决这个问题。

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

编辑:

我试图将我的代码修改为以下内容:
<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>

但是随着按钮边框的尺寸也增加了,所以在大屏幕上,按钮边框变成了一厘米宽。

最佳答案

试试这个:像你在其他任何时候一样渲染你的文本项目:

  • TextBlock s 包含在“ItemsControl”中?
  • ListBox ?

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

    关于WPF 字体缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1706674/

    相关文章:

    java - 通过循环数组列表来填充 setter

    r - 在 R 中缩放 Flexdashboard 仪表

    c# - 如何绑定(bind)到 MVVM 中的密码框

    wpf - 测试 Controller 上部署的文件丢失 (TFS 2013)

    c# - 在 WPF 的代码隐藏中为 ListBox 创建 ItemTemplate - 第 II 部分

    java - Libgdx 相机/缩放问题

    html - 如何使幻灯片图像在不同的视口(viewport)尺寸下按比例缩放?

    c# - Wpf ItemSsource 不更新自定义控件中的视觉控件

    java - 如何在 Android 中创建有用的工具提示

    c++ - QInputDialog 操作取决于 ComboBox 中的项目选择