是否可以知道网格中的单元数?我可以通过某种方式访问它们以将孩子添加到每个孩子中吗?
例如,如果我有2行x 3列的网格:
<Grid Name="myGrid">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
</Grid>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
</Grid>
</Grid>
我尝试过类似的事情:
foreach (RowDefinition row in this.myGrid.RowDefinitions)
{
foreach(ColumnDefinition col in row)
{
// This doesn't work.
// There is no property ColumnDefinitions in row.
}
}
我不知道该如何实现(如果可能的话)。
答案 0 :(得分:2)
RowDefinitions和ColumnDefinitions属于Grid,它们彼此独立。
修复xaml定义以创建2x3网格(而不是当前的2x1、1x3、1x3网格)
from sys import argv
import os
script = argv
prompt = '> '
# If I use input for the user_name, when running the var script it will show the file script plus user_name
print("Hello Master. Please tell me your name: ")
user_name = input(prompt)
script = os.path.basename(__file__)
print(f"Hi {user_name}, I'm the {script} script.")
网格单元格没有任何控件表示。它们只是网格中的矩形区域,是在排列期间确定的。使用<Grid Name="myGrid">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
</Grid>
和Grid.RowProperty
附加的DP在网格中放置子元素:
Grid.ColumnProperty
如果所有单元格的大小相同,则可以使用UniformGrid:
for(int r = 0; r < myGrid.RowDefinitions.Count; r++)
{
for(int c = 0; c < myGrid.ColumnDefinitions.Count; c++)
{
var B = new Border { Margin = new Thickness(5), Background = Brushes.Green };
B.SetValue(Grid.RowProperty, r);
B.SetValue(Grid.ColumnProperty, c);
myGrid.Children.Add(B);
}
}