0

我有一个有几个TextBlocks 的面板。我想修改这个面板,使每个 TextBlock 的字体大小根据面板的大小而变化。

当用户改变窗口的大小(例如最大化或恢复它)时,面板的大小可以改变。

我尝试使用Viewbox

<Viewbox Stretch="Fill">
    <Grid Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Viewbox}}, Path=Width}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <TextBlock 
            HorizontalAlignment="Stretch"
            VerticalAlignment="Stretch" />
        <TextBlock 
            HorizontalAlignment="Stretch"
            VerticalAlignment="Stretch" />
        <TextBlock 
            HorizontalAlignment="Stretch"
            VerticalAlignment="Stretch" />
        <TextBlock 
            HorizontalAlignment="Stretch"
            VerticalAlignment="Stretch" />
        <TextBlock 
            HorizontalAlignment="Stretch"
            VerticalAlignment="Stretch" />
        <TextBlock 
            HorizontalAlignment="Stretch"
            VerticalAlignment="Stretch" />          
    </Grid>
</Viewbox>

但它不起作用,因为

  • 字体大小不会变大,当大小Viewbox增加时
  • 当尺寸减小时,它不会变小Viewbox

如何实现这些文本块的缩放(以便它们填充整个可用区域,并调整它们的字体大小)?

4

2 回答 2

1

您有自动高度宽度并且您没有将文本块放在网格中

<Grid>
    <Viewbox Stretch="Fill">
        <Grid Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Viewbox}}, Path=Width}">
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Column="0"  Text="TextBlock1"
                HorizontalAlignment="Stretch"
                VerticalAlignment="Stretch" />
            <TextBlock Grid.Column="1"  Text="TextBlock2"
                HorizontalAlignment="Stretch"
                VerticalAlignment="Stretch" />
            <TextBlock Grid.Column="2"  Text="TextBlock3" 
                HorizontalAlignment="Stretch"
                VerticalAlignment="Stretch" />
        </Grid>
    </Viewbox>
</Grid>
于 2013-09-04T14:48:42.480 回答
0

AViewbox 确实有效......这几乎就是它的用途。试试这个简单的例子:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="350" Width="525">
    <Viewbox Stretch="Fill">
        <TextBlock Text="This will change size" FontSize="40" />
    </Viewbox>
</Window>

我认为问题在于您没有将初始设置FontSize足够大并且您的Stretch模式不正确。如果此示例不太适合您的要求,请尝试使用其他Stretch模式。

于 2013-09-04T12:23:00.483 回答