我有一个动态网格,可以根据提供的行和列的大小进行调整。但是,即使在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));
}
}
答案 0 :(得分: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);