0

So I've written some code that creates TextBlocks from a list of strings by calling a for loop:

List<string> menuPages = new List<string>() { "Home", "Media", "Settings" };
//method called from constructor:
private void createHeaders ()
{
    for (int i=0; i<menuPages.Count; i++)
    {
        TextBlock iheader = new TextBlock();
        iheader.Name = menuPages[i];
        iheader.Text = menuPages[i];
        if (i==pageIndex)
        { iheader.FontSize = 36; }
        else
        { iheader.FontSize = 32; }
        stacky.Children.Add(iheader); //Adding button to stack panel
    }
}

Now I've been writing another method that would cycle through each TextBlock in a loop and change the text to whatever I intend. I'd gotten a foreach loop working for the stackPanel children: (TextBlock tBlock in stacky.Children) but I need to work with an indexed for loop. The code below is how I WANT to achieve this:

//Re-render headers
for (int i = 0; i < menuPages.Count; i++)
{
    //TextBlock menuPages[i].text = "foo";
}

Now of course the syntax above doesn't work so my question is, how can I address the TextBlocks from the strings in a list?

2 Answers 2

1

Just have your textblocks created in a list. So, you can manipulate easily with indexed forloop.

List<string> menuPages = new List<string>() { "Home", "Media", "Settings" };
List<TextBlock> textBlocks = new List<TextBlock>();

public MainPage()
{
    this.InitializeComponent();
    createHeaders();
}

private void createHeaders()
{
    for (int i = 0; i < menuPages.Count; i++)
    {
        TextBlock iheader = new TextBlock();
        iheader.Name = menuPages[i];
        iheader.Text = menuPages[i];              
        iheader.FontSize = 32;
        textBlocks.Add(iheader);
        Stacky.Children.Add(iheader); 
    }
}

private void change_Click(object sender, RoutedEventArgs e)
{
    for (int i = 0; i < textBlocks.Count; i++)
    {
        textBlocks[i].Text = "foo";
    }
} 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! It seems so obvious now making a second list but I looked past that as I figured it wouldn't register. Very pleased to have learnt this :)
1

If the StackPanel only contains the TextBlock elements you are adding dynamically, you can access them this way as well:

foreach (var textBlock in Stacky.Children.OfType<TextBlock>())
{
   textBlock.Text = "something";
}

This approach uses the OfType<T> LINQ extension method which filters the input collection by the specified type, so it only returns those children of Stacky that are a TextBlock.

If you have more content, in the StackPanel, then @Vignesh G's answer is the way to go.

1 Comment

Didn't quite address my need for indexing so I ended up using Vignesh's solution. But thank you! It did help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.