gpt4 book ai didi

WPF 字体缩放

转载 作者:行者123 更新时间:2023-12-04 08:56:26 25 4
gpt4 key购买 nike

我有一个 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/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com