WPF Square 自动调整父容器大小

发布于 2024-12-23 15:34:07 字数 854 浏览 6 评论 0原文

我的 WPF 项目中有一个 UniformGrid 对象,该对象有 2 行和 3 列,其宽度和高度设置为自动(两种对齐方式都设置为拉伸)。

该网格将容纳 6 个正方形,我希望尽可能多地填充它们的单元格,并水平和垂直居中。

我需要添加什么才能允许方块根据父级的动态大小增加/减少其长度/宽度? IE,当调整窗口大小时。

到目前为止,这是我的 xaml:

    <UniformGrid Rows="2" Columns="3">
        <Rectangle Fill="#FFF4F4F5" Height="100" Stroke="Black" Width="100"/>
        <Rectangle Fill="#FFF4F4F5" Height="100" Stroke="Black" Width="100"/>
        <Rectangle Fill="#FFF4F4F5" Height="100" Stroke="Black" Width="100"/>
        <Rectangle Fill="#FFF4F4F5" Height="100" Stroke="Black" Width="100"/>
        <Rectangle Fill="#FFF4F4F5" Height="100" Stroke="Black" Width="100"/>
        <Rectangle Fill="#FFF4F4F5" Height="100" Stroke="Black" Width="100"/>
    </UniformGrid>

编辑:

矩形 对象需要保持正方形。

I have a UniformGrid object in my WPF project that has 2 rows and 3 cols and its width and height are set to auto (with both alignments set to Stretch).

This grid will hold 6 squares that I want to fill as much of their cell as possible and be centered horizontally and vertically.

What do I need to be added to allow for the squares to increase/decrease their length/width based on the dynamic size of the parent? I.E., when the window is resized.

Here is my xaml so far:

    <UniformGrid Rows="2" Columns="3">
        <Rectangle Fill="#FFF4F4F5" Height="100" Stroke="Black" Width="100"/>
        <Rectangle Fill="#FFF4F4F5" Height="100" Stroke="Black" Width="100"/>
        <Rectangle Fill="#FFF4F4F5" Height="100" Stroke="Black" Width="100"/>
        <Rectangle Fill="#FFF4F4F5" Height="100" Stroke="Black" Width="100"/>
        <Rectangle Fill="#FFF4F4F5" Height="100" Stroke="Black" Width="100"/>
        <Rectangle Fill="#FFF4F4F5" Height="100" Stroke="Black" Width="100"/>
    </UniformGrid>

Edit:

And the Rectangle objects need to remain square.

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

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

发布评论

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

评论(3

芸娘子的小脾气 2024-12-30 15:34:07
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
  <Grid>
<UniformGrid Rows="2" Columns="3">
<Viewbox Stretch="Uniform"><Rectangle Height="100" Width="100" Fill="#FFF4F4F5" Stroke="Black" /></Viewbox>
<Viewbox Stretch="Uniform"><Rectangle Height="100" Width="100" Fill="#FFF4F4F5" Stroke="Black" /></Viewbox>
<Viewbox Stretch="Uniform"><Rectangle Height="100" Width="100" Fill="#FFF4F4F5" Stroke="Black" /></Viewbox>
<Viewbox Stretch="Uniform"><Rectangle Height="100" Width="100" Fill="#FFF4F4F5" Stroke="Black" /></Viewbox>
<Viewbox Stretch="Uniform"><Rectangle Height="100" Width="100" Fill="#FFF4F4F5" Stroke="Black" /></Viewbox>
<Viewbox Stretch="Uniform"><Rectangle Height="100" Width="100" Fill="#FFF4F4F5" Stroke="Black" /></Viewbox>
    </UniformGrid>
  </Grid>
</Page>
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
  <Grid>
<UniformGrid Rows="2" Columns="3">
<Viewbox Stretch="Uniform"><Rectangle Height="100" Width="100" Fill="#FFF4F4F5" Stroke="Black" /></Viewbox>
<Viewbox Stretch="Uniform"><Rectangle Height="100" Width="100" Fill="#FFF4F4F5" Stroke="Black" /></Viewbox>
<Viewbox Stretch="Uniform"><Rectangle Height="100" Width="100" Fill="#FFF4F4F5" Stroke="Black" /></Viewbox>
<Viewbox Stretch="Uniform"><Rectangle Height="100" Width="100" Fill="#FFF4F4F5" Stroke="Black" /></Viewbox>
<Viewbox Stretch="Uniform"><Rectangle Height="100" Width="100" Fill="#FFF4F4F5" Stroke="Black" /></Viewbox>
<Viewbox Stretch="Uniform"><Rectangle Height="100" Width="100" Fill="#FFF4F4F5" Stroke="Black" /></Viewbox>
    </UniformGrid>
  </Grid>
</Page>
夏の忆 2024-12-30 15:34:07

您可以这样做:

<UniformGrid.Resources>
    <Style TargetType="Rectangle">
        <Setter Property="Width"
                Value="{Binding RelativeSource={RelativeSource Mode=Self},Path=ActualHeight}" />
    </Style>
</UniformGrid.Resources>

或者您可以将高度绑定到实际宽度。

不幸的是,这不会让它们发挥到最大程度。

You could do this:

<UniformGrid.Resources>
    <Style TargetType="Rectangle">
        <Setter Property="Width"
                Value="{Binding RelativeSource={RelativeSource Mode=Self},Path=ActualHeight}" />
    </Style>
</UniformGrid.Resources>

Or you could bind the Height to the ActualWidth.

Unfortunately this will not keep them stretched to the maximum.

み零 2024-12-30 15:34:07

如果您删除 heightwidth 属性,它就会这样做。

If you remove the height and width properties it will do just that.

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