我在XAML中製作了一個5x5的網格,其中每行放置了一個TextBlock和一個邊框5次。它看起來像這樣:image無法用[]將索引應用於'Border'類型的表達式
我想要創建一個動畫,通過行來顯示用戶它正在檢查輸入。 (只是爲了使它看起來程序正在做某些事情,實際檢查發生的方式不同)。
第一行(我砍最後的邊界段休息後關閉)的XAML代碼:
<Grid BorderThickness="1" BorderBrush="Black">
<Grid Margin="10" BorderThickness="1" BorderBrush="White" Width="350" Grid.RowSpan="4" Grid.ColumnSpan="2" Height="450">
<Grid.Resources>
<SolidColorBrush x:Key="brush" Color="Silver"/>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Border x:Name="borderRow1Column1" Grid.Column="0" Grid.Row="0" BorderBrush="{StaticResource brush}" BorderThickness="1" Width="50" Height="50">
<TextBlock x:Name="tbRow1column1" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="24"></TextBlock>
</Border>
<Border x:Name="borderRow1Column2" Grid.Column="1" Grid.Row="0" BorderBrush="{StaticResource brush}" BorderThickness="1" Width="50" Height="50">
<TextBlock x:Name="tbRow1column2" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="24"></TextBlock>
</Border>
<Border x:Name="borderRow1Column3" Grid.Column="2" Grid.Row="0" BorderBrush="{StaticResource brush}" BorderThickness="1" Width="50" Height="50">
<TextBlock x:Name="tbRow1column3" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="24" ></TextBlock>
</Border>
<Border x:Name="borderRow1Column4" Grid.Column="3" Grid.Row="0" BorderBrush="{StaticResource brush}" BorderThickness="1" Width="50" Height="50">
<TextBlock x:Name="tbRow1column4" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="24"></TextBlock>
</Border>
<Border x:Name="borderRow1Column5" Grid.Column="4" Grid.Row="0" BorderBrush="{StaticResource brush}" BorderThickness="1" Width="50" Height="50">
<TextBlock x:Name="tbRow1column5" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="24"></TextBlock>
</Border>
<!-- More Rows following after this one -->
</Grid>
現在,我想有一個循環,通過該行中的每個文本塊去,和而不是複製粘貼它,我想通過具有索引的for循環來減少代碼。
private void displayCheckAnimation()
{
Color gray = Color.FromArgb(255, 128, 128, 128);
Color black = Color.FromArgb(255, 255, 255, 255);
Border borderRow1Column = new Border();
for (int i = 0; i < 5; i++)
{
borderRow1Column[i].Background = new SolidColorBrush(gray);
Task.Delay(50).Wait();
borderRow1Column[i].Background = new SolidColorBrush(black);
Task.Delay(50).Wait();
}
}
但是,這給我的錯誤
CS0021 Cannot apply indexing with [] to an expression of type 'Border'
的任何解決方案,我怎麼可以通過我行的循環?