WPF Square 自动调整父容器的大小

标签 wpf user-interface autolayout resize autoresize

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

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

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

到目前为止,这是我的 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>

编辑:

Rectangle 对象需要保持正方形。

最佳答案

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

关于WPF Square 自动调整父容器的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8682345/

相关文章:

c# - 无法创建 "MainViewModel"的实例

c# - 更改 DataGrid 的列宽

ios - ImageView 坚持 UITableViewCell 中的 UILabel

wpf - Avalon Dock float 窗口 - ShowInTaskBar

wpf - 在 WPF CAL MVVM 中初始化模型和 View 的正确方法是什么

java - 以编程方式创建编辑文本并将其对齐到 TextView 的右侧

java - 使用 Eclipse RCP 开发 Workbench 应用程序::它能变得多丰富?

java - 使用 Groovy/JRuby/Jython 或其他 JVM 语言的 UI 设计替代方案?

swift autoLayout 覆盖 CGRectMake

ios - 如何理解自动布局约束的第一项和第二项?