按钮未占满 space
Buttons not taking up full space
我有一个动态网格,可以根据提供的行和列大小进行调整。但是,我似乎无法将按钮 spaced out correctly/take 放在网格的所有 space 中,即使在 XAML 中我已将其设置为一些。
这是目前的样子:
设置 height/width 和方向的代码行是:
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));
}
}
我认为你必须改变
<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);
我有一个动态网格,可以根据提供的行和列大小进行调整。但是,我似乎无法将按钮 spaced out correctly/take 放在网格的所有 space 中,即使在 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));
}
}
我认为你必须改变
<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);