我的网格有很多行和列。
看起来像这样。
<Grid x:Name="grid" Grid.ColumnSpan="3" RenderTransformOrigin="0.5,0.5">
<Grid.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Grid.RenderTransform>
<Grid.RowDefinitions>
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<rectImg:RectImg Grid.Row="0" Grid.Column="0"></rectImg:RectImg>
<rectImg:RectImg Grid.Row="0" Grid.Column="1"></rectImg:RectImg>
<rectImg:RectImg Grid.Row="0" Grid.Column="2"></rectImg:RectImg>
<rectImg:RectImg Grid.Row="0" Grid.Column="3"></rectImg:RectImg>
<rectImg:RectImg Grid.Row="0" Grid.Column="4"></rectImg:RectImg>
<rectImg:RectImg Grid.Row="0" Grid.Column="5"></rectImg:RectImg>
<rectImg:RectImg Grid.Row="0" Grid.Column="6"></rectImg:RectImg>
<rectImg:RectImg Grid.Row="0" Grid.Column="7"></rectImg:RectImg>
<rectImg:RectImg Grid.Row="0" Grid.Column="8"></rectImg:RectImg>
<rectImg:RectImg Grid.Row="0" Grid.Column="9"></rectImg:RectImg>
<rectImg:RectImg Grid.Row="0" Grid.Column="9"></rectImg:RectImg>
<rectImg:RectImg Grid.Row="0" Grid.Column="9"></rectImg:RectImg>
<rectImg:RectImg Grid.Row="0" Grid.Column="9"></rectImg:RectImg>
<rectImg:RectImg Grid.Row="0" Grid.Column="10"></rectImg:RectImg>
<rectImg:RectImg Grid.Row="0" Grid.Column="11"></rectImg:RectImg>
<rectImg:RectImg Grid.Row="0" Grid.Column="12"></rectImg:RectImg>
<rectImg:RectImg Grid.Row="0" Grid.Column="13"></rectImg:RectImg>
<rectImg:RectImg Grid.Row="0" Grid.Column="14"></rectImg:RectImg>
<rectImg:RectImg Grid.Row="0" Grid.Column="15"></rectImg:RectImg>
<rectImg:RectImg Grid.Row="1" Grid.Column="0"></rectImg:RectImg>
<rectImg:RectImg Grid.Row="2" Grid.Column="0"></rectImg:RectImg>
<rectImg:RectImg Grid.Row="3" Grid.Column="0"></rectImg:RectImg>
<rectImg:RectImg Grid.Row="4" Grid.Column="0"></rectImg:RectImg>
<rectImg:RectImg Grid.Row="5" Grid.Column="0"></rectImg:RectImg>
</Grid>
我想初始化代码并使用foreach或代码:
<rectImg:RectImg Grid.Row="0" Grid.Column="0"></rectImg:RectImg>
我尝试了很多方法。但我没有这样做..许多人说“使用Grid.SetColumn和Grid.SetRow”但它无法应用....
我该如何解决这个问题?
答案 0 :(得分:0)
为了您的要求,可以按照以下方式完成......
从后面的代码中,您可以在网格子项中添加RectImg控件,在此之前确保分配行和列索引。
private void AddRectImgControls()
{
var rectImg = new RectImg();
for(var i = 0; i++; i < this.grid.RowDefinitions.Count)
{
for(var j = 0; j++; j < this.grid.ColumnDefinitions.Count)
{
var rectImg = new RectImg();
Grid.SetRow(rectImg, i);
Grid.SetColumn(rectImg, j);
this.grid.Children.Add(rectImg);
}
}
}
但这种设计并不高效。它实际上应该是一个ItemsControl,其中包含多个RectImg控件。
像这样.... <ItemsControl ItemsSource="{x:Static local:MainWindow.UniformGridCollection}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Rows="8" Columns="23" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<rectImg:RectImg/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
这里“local”是MainWindow所在的本地项目命名空间的XAML命名空间。
您的MainWindow应如下所示......
public partial class MainWindow : Window
{
//// .... rest of the MainWindow code.
public static List<object> UniformGridCollection
{
get
{
var coll = new List<object>();
for(i = 0; i < 8 * 23; i++) //// 8 and 23 are UniformGrid's row and column size.
{
coll.Add(new object());
}
return coll;
}
}
}
如果有帮助,请告诉我。