我有一个包含多个TextBlock
的面板。我想修改此面板,使每个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
的大小减小时,它不会变小。如何实现这些文本块的缩放(以便它们填充整个可用区域并调整其字体大小)?
答案 0 :(得分: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>
答案 1 :(得分:0)
Viewbox
工作......这就是它的用途。试试这个简单的例子:
<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
模式。