WPF / XAML - 将文本大小缩放到窗口大小

时间:2009-07-16 15:27:50

标签: wpf xaml scaling

我是WPF的新手。我有一个带有一堆标签的WPF窗口以及一个ListBox。

调整窗口大小时,我想缩放某些标签的大小,但不是全部。我不希望ListBox扩展 - 只是一些标签。

我知道我可以在Window调整大小时使用Viewbox来调整大小,但是尽管我搞砸了,但是我没有达到预期的效果。当然我不能用Viewbox包围整个东西,因为这会调整一切,所以我想我必须在整个窗口中放下一堆不同的Viewbox,围绕我要扩展的每个标签。但当然......当我这样做时,根本没有任何扩展。

同样,当我展开标签时,还有其他标签需要保留在这些标签旁边,因为它们是标识符。

所以......这是我现在的XAML。我甚至不知道我是否走在正确的道路上。任何帮助制作带有数字的标签随窗口扩展都将受到赞赏。

<Window x:Class="WpfApplication7.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication7"
    Title="Window1">
    <StackPanel Orientation="Horizontal">
        <ListBox Margin="2">
            <ListBoxItem>a</ListBoxItem>
            <ListBoxItem>b</ListBoxItem>
            <ListBoxItem>c</ListBoxItem>
        </ListBox>
        <StackPanel Orientation="Vertical">
            <Label>Title</Label>
            <StackPanel Orientation="Horizontal">
                <Grid>
                    <Grid.RowDefinitions><RowDefinition/><RowDefinition/><RowDefinition/><RowDefinition/></Grid.RowDefinitions>
                    <Label Grid.Row="0">A</Label>
                    <Label Grid.Row="1">B</Label>
                    <Label Grid.Row="2">C</Label>
                    <Label Grid.Row="3">D</Label>
                </Grid>
                <Grid>
                    <Grid.RowDefinitions><RowDefinition/><RowDefinition/><RowDefinition/><RowDefinition/></Grid.RowDefinitions>
                    <Viewbox Grid.Row="0" Stretch="Fill">
                        <Label>1</Label>
                    </Viewbox>
                    <Viewbox Grid.Row="1" Stretch="Fill">
                        <Label>2</Label>
                    </Viewbox>
                    <Viewbox Grid.Row="2" Stretch="Fill">
                        <Label>3</Label>
                    </Viewbox>
                    <Viewbox Grid.Row="3" Stretch="Fill">
                        <Label>4</Label>
                    </Viewbox>
                </Grid>
                <Grid>
                    <Grid.RowDefinitions><RowDefinition/><RowDefinition/><RowDefinition/><RowDefinition/></Grid.RowDefinitions>
                    <Viewbox Grid.Row="0" Stretch="Fill">
                        <Label>5</Label>
                    </Viewbox>
                    <Viewbox Grid.Row="1" Stretch="Fill">
                        <Label>6</Label>
                    </Viewbox>
                    <Viewbox Grid.Row="2" Stretch="Fill">
                        <Label>7</Label>
                    </Viewbox>
                    <Viewbox Grid.Row="3" Stretch="Fill">
                        <Label>8</Label>
                    </Viewbox>
                </Grid>
                <Grid>
                    <Grid.RowDefinitions><RowDefinition/><RowDefinition/><RowDefinition/><RowDefinition/></Grid.RowDefinitions>
                    <Label Grid.Row="0">E</Label>
                    <Label Grid.Row="1">F</Label>
                    <Label Grid.Row="2">G</Label>
                    <Label Grid.Row="3">H</Label>
                </Grid>
            </StackPanel>
        </StackPanel>
    </StackPanel>
</Window>

<Window x:Class="WpfApplication7.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplication7" Title="Window1"> <StackPanel Orientation="Horizontal"> <ListBox Margin="2"> <ListBoxItem>a</ListBoxItem> <ListBoxItem>b</ListBoxItem> <ListBoxItem>c</ListBoxItem> </ListBox> <StackPanel Orientation="Vertical"> <Label>Title</Label> <StackPanel Orientation="Horizontal"> <Grid> <Grid.RowDefinitions><RowDefinition/><RowDefinition/><RowDefinition/><RowDefinition/></Grid.RowDefinitions> <Label Grid.Row="0">A</Label> <Label Grid.Row="1">B</Label> <Label Grid.Row="2">C</Label> <Label Grid.Row="3">D</Label> </Grid> <Grid> <Grid.RowDefinitions><RowDefinition/><RowDefinition/><RowDefinition/><RowDefinition/></Grid.RowDefinitions> <Viewbox Grid.Row="0" Stretch="Fill"> <Label>1</Label> </Viewbox> <Viewbox Grid.Row="1" Stretch="Fill"> <Label>2</Label> </Viewbox> <Viewbox Grid.Row="2" Stretch="Fill"> <Label>3</Label> </Viewbox> <Viewbox Grid.Row="3" Stretch="Fill"> <Label>4</Label> </Viewbox> </Grid> <Grid> <Grid.RowDefinitions><RowDefinition/><RowDefinition/><RowDefinition/><RowDefinition/></Grid.RowDefinitions> <Viewbox Grid.Row="0" Stretch="Fill"> <Label>5</Label> </Viewbox> <Viewbox Grid.Row="1" Stretch="Fill"> <Label>6</Label> </Viewbox> <Viewbox Grid.Row="2" Stretch="Fill"> <Label>7</Label> </Viewbox> <Viewbox Grid.Row="3" Stretch="Fill"> <Label>8</Label> </Viewbox> </Grid> <Grid> <Grid.RowDefinitions><RowDefinition/><RowDefinition/><RowDefinition/><RowDefinition/></Grid.RowDefinitions> <Label Grid.Row="0">E</Label> <Label Grid.Row="1">F</Label> <Label Grid.Row="2">G</Label> <Label Grid.Row="3">H</Label> </Grid> </StackPanel> </StackPanel> </StackPanel> </Window>

1 个答案:

答案 0 :(得分:2)

你走在正确的道路上。但是,您需要使用一些列定义,并且您的行定义有点不稳定。您正在使用彼此嵌入的一堆不同的布局面板,这会影响Viewbox内置的大小调整。您可以使用一个简单的5x5网格(无堆栈面板)完成相同的布局。

我已在以下XAML中证明了这一点:

<Window x:Class="TestWpfApplication.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestWpfApplication"
Title="Window1">
<Window.Resources>
    <Style TargetType="{x:Type Label}" x:Key="{x:Type Label}">
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
    </Style>
</Window.Resources>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>

    <ListBox Grid.RowSpan="5" Grid.Column="0">
        <ListBoxItem>a</ListBoxItem>
        <ListBoxItem>b</ListBoxItem>
        <ListBoxItem>c</ListBoxItem>
    </ListBox>

    <Label Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="4">Title</Label>

    <Label Grid.Row="1" Grid.Column="1">A</Label>
    <Label Grid.Row="2" Grid.Column="1">B</Label>
    <Label Grid.Row="3" Grid.Column="1">C</Label>
    <Label Grid.Row="4" Grid.Column="1">D</Label>

    <Viewbox Grid.Row="1" Grid.Column="2">
        <Label>1</Label>
    </Viewbox>
    <Viewbox Grid.Row="2" Grid.Column="2">
        <Label>2</Label>
    </Viewbox>
    <Viewbox Grid.Row="3" Grid.Column="2">
        <Label>3</Label>
    </Viewbox>
    <Viewbox Grid.Row="4" Grid.Column="2">
        <Label>4</Label>
    </Viewbox>

    <Viewbox Grid.Row="1" Grid.Column="3">
        <Label>5</Label>
    </Viewbox>
    <Viewbox Grid.Row="2" Grid.Column="3">
        <Label>6</Label>
    </Viewbox>
    <Viewbox Grid.Row="3" Grid.Column="3">
        <Label>7</Label>
    </Viewbox>
    <Viewbox Grid.Row="4" Grid.Column="3">
        <Label>8</Label>
    </Viewbox>

    <Label Grid.Row="1" Grid.Column="4">E</Label>
    <Label Grid.Row="2" Grid.Column="4">F</Label>
    <Label Grid.Row="3" Grid.Column="4">G</Label>
    <Label Grid.Row="4" Grid.Column="4">H</Label>
</Grid>