在UWP中创建在运行时随2D数组一起更改的Grid,可以通过以下步骤实现:
<Grid x:Name="myGrid">
<!-- Grid的布局和样式 -->
</Grid>
private ObservableCollection<ObservableCollection<int>> myArray = new ObservableCollection<ObservableCollection<int>>();
public MainPage()
{
InitializeComponent();
// 初始化2D数组
for (int i = 0; i < 3; i++)
{
ObservableCollection<int> row = new ObservableCollection<int>();
for (int j = 0; j < 3; j++)
{
row.Add(0);
}
myArray.Add(row);
}
// 将2D数组与Grid绑定
myGrid.ItemsSource = myArray;
}
<Grid x:Name="myGrid">
<ItemsControl ItemsSource="{x:Bind myArray}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Grid.Column="{Binding Path=Index}">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Text="{Binding [0]}" Grid.Row="0" />
<TextBlock Text="{Binding [1]}" Grid.Row="1" />
<TextBlock Text="{Binding [2]}" Grid.Row="2" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
通过以上步骤,你可以在UWP中创建一个与2D数组动态绑定的Grid,使其能够在运行时随数组的更改而更新显示内容。
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云