【发布时间】: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));
}
}
【问题讨论】:
-
有人有什么想法吗?