【问题标题】:Buttons not taking up full space按钮未占用全部空间
【发布时间】:2019-12-12 09:20:41
【问题描述】:

我有一个动态网格,可以根据提供的行和列大小进行调整。但是,即使在 XAML 中我已将其设置为执行某些操作,我似乎也无法正确间隔开按钮/占用网格的所有空间。

这是目前的样子: 设置高度/宽度和方向的代码行是:

        button.HorizontalAlignment = HorizontalAlignment.Stretch;
        button.VerticalAlignment = VerticalAlignment.Stretch;
        button.Name = "button" + i.ToString();
        button.Margin = new Thickness(10);
        button.Padding = new Thickness(2);
        var potentialHeight = (wellGrid.Height / wellGrid.ColumnDefinitions.Count) - (button.Margin.Top * 2);
        var potentialWidth = (wellGrid.Width / wellGrid.RowDefinitions.Count) - (button.Margin.Left * 2);
        button.Height = button.Width = (potentialHeight < potentialWidth) ? potentialHeight : potentialWidth;

XAML 是这样的:

                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <Grid DockPanel.Dock="Top" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" x:Name="wellGrid" Grid.Row="1" Width="800" Height="550"
                  local:GridHelpers.RowCount="{Binding RowCount}"
                  local:GridHelpers.ColumnCount="{Binding ColumnCount}">
                </Grid>

帮助回答的附加代码...

                int i = 0;
                for (int row = 0; row < grid.RowDefinitions.Count; ++row)
                {
                    for (int column = 0; column < grid.ColumnDefinitions.Count; ++column)
                    {
                        i++;
                        grid.Children.Add(CreateGridButton(i, column, row));
                    }
                }

【问题讨论】:

  • 有人有什么想法吗?

标签: wpf grid controls


【解决方案1】:

我认为你必须改变

 <Grid.RowDefinitions>
     <RowDefinition Height="Auto"/>
     <RowDefinition Height="Auto"/>
 </Grid.RowDefinitions>

 <Grid.RowDefinitions>
     <RowDefinition Height="*"/>
     <RowDefinition Height="*"/>
 </Grid.RowDefinitions>

auto 表示按列内容的大小

* 表示与网格成比例的大小

这里不会完全帮助您,但是如果您想实现该行为,那么您有 2 个选项(我相信还有更多选项)

<UniformGrid Rows="{Binding YOUR_ROW_COUNT}"
             Columns="{Binding YOUR_COL_COUNT}">
</UniformGrid>
  • 添加控件后将添加到您的网格中
var length = new GridLength(1, GridUnitType.Star);
var col = new ColumnDefinition() { Width = length };
var row = new RowDefinition() { Height= length };

grid.RowDefinitions.Add(row);
grid.ColumnDefinitions.Add(col);

然后你必须像这样添加你的控件

//example
Grid.SetRow(control, 0);
Grid.SetColumn(control, 2);

【讨论】:

  • 更改为唯一网格会破坏代码...将尝试其他选项...
  • 不知道如何才能使第二个选项起作用...我不知道要在代码中更改什么...我添加了更多代码来提供帮助
  • @slickchick2 它与我刚刚测试过的 UniformGrid 完美配合。你不需要任何额外的 GridHelper ,只需用 UniformGrid 替换你的 Grid 并在那里添加你的控件。如果你想让它们填满,那么删除你的 Col 和 Row 绑定。
  • 哇,你是一个完整的传奇!抱歉,我是一个普通人,但仍然有我的 local:gridhelper.columncount 而不是 Rows/Columns 用于唯一网格。惊人的!!!!我想知道唯一网格和网格之间的区别到底是什么:\
  • @slickchick2 uniformgrid 只是另一个“自动”为您生成列和行的面板。提示:如果您想知道这些东西是如何编程的,请查看referencesource.microsoft.com/#PresentationFramework/src/…
猜你喜欢
  • 2018-06-09
  • 2021-01-21
  • 2020-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多