I can't figure out how to call several similarly named WPF TextBlocks in a for loop.
In WPF I have several TextBlocks that are each inside of a grid cell:
<TextBlock Name="Banner0" />
<TextBlock Name="Banner1" />
<TextBlock Name="Banner2" />
For example those three are in Grid.Row="0" Grid.Column="0", Grid.Row="0" Grid.Column="1", Grid.Row="0" Grid.Column="2", respectively.
In C# I am able to change the text in the above TextBlocks with the following code:
Banner0.Text = "Sample Text";
Banner1.Text = "Sample Text";
Banner2.Text = "Sample Text";
However what I want to do, but can't figure out how to do is change it this way.
for (int i = 0; i < 3; i++)
{
Banneri.Text = "Sample Text";
}
I understand why I can't do Banneri, but can't figure out how to achieve that concept within a for loop.
Also the text will not be the same in each TextBlock, but for simplicity I used "Sample Text" in all three, as this is not the area that is causing difficulty.
var textBlocks = new TextBlock[] { Banner0, Banner1, Banner2 }
should also work. – Clemensforeach (var textblock in panel.Children.OfType<TextBlock>()) { … }
– Clemens