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

发布于 2024-07-29 02:56:15 字数 3893 浏览 4 评论 0原文

我是 WPF 新手。 我有一个 WPF 窗口,上面有一堆标签以及一个列表框。

调整窗口大小时,我想缩放某些标签的大小,但不是全部。 我也不希望列表框缩放——只是一些标签。

我知道我可以使用 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>

>

I'm new to WPF. I have a WPF Window with a bunch of Labels on it as well as a ListBox.

When resizing the window, I want to scale the size of SOME of the Labels, but not all of them. I don't want the ListBox to scale either--just some Labels.

I understand I can use a Viewbox to resize as the Window resizes, but as much as I mess with it, I'm not getting the desired effect. Of course I can't surround the entire thing with a Viewbox because that would resize everything, so I figured I would have to drop a bunch of different Viewboxes throughout the Window surrounding each label I want to expand. But of course... nothing expands at all when I do this.

Along the same lines, when I expand the labels, there are other labels that need to remain right next to those labels because they are identifiers.

So... here's the XAML I have at this point. I don't even know if I'm on the right path. Any help making the labels with number in them expand with the window would be appreciated.

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无法回应 2024-08-05 02:56:15

你走在正确的道路上。 但是,您需要使用一些列定义,并且您的行定义有点不稳定。 您正在使用一堆相互嵌入的不同布局面板,这会影响 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>

You're on the right path. However, you need to use some column definitions, and your row definitions are a bit wonky. You're using a bunch of different layout panels embedded in each other, which is affecting the Viewbox built-in resizing. You can accomplish this exact same layout with a single, simple, 5x5 grid (no stackpanels).

I have demonstrated this in the following 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>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文