1

I made a 3x3 Grid filled with buttons.
I need to group them somehow to use Foreach on them.
How can I do that?

I searched the internet but didn't found anything helpful.
I have one more button that should never change so calling all buttons to change isn't an answer.

ekad
  • 14,436
  • 26
  • 44
  • 46
Szymon
  • 460
  • 6
  • 17

3 Answers3

5

You should really look into implementing a MVVM pattern, in most cases you won't need to reference any buttons since it would be part of your ViewModel and you can loop through the collection that you want to change.

However, given your information you have something like this 3x3 with buttons in each one?

<Grid x:Name="myGrid">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="33*"/>
        <ColumnDefinition Width="33*"/>
        <ColumnDefinition Width="33*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="33*"/>
        <RowDefinition Height="33*"/>
        <RowDefinition Height="33*"/>
    </Grid.RowDefinitions>
    <Button Content="1" Grid.Row="0" Grid.Column="0" ></Button>
    <Button Content="2" Grid.Row="0" Grid.Column="1" ></Button>
    <Button Content="3" Grid.Row="0" Grid.Column="2" ></Button>
    <Button Content="4" Grid.Row="1" Grid.Column="0" ></Button>
    <Button Content="5" Grid.Row="1" Grid.Column="1" ></Button>
    <Button Content="6" Grid.Row="1" Grid.Column="2" ></Button>
    <Button Content="7" Grid.Row="2" Grid.Column="0" ></Button>
    <Button Content="8" Grid.Row="2" Grid.Column="1" ></Button>
    <Button Content="9" Grid.Row="2" Grid.Column="2" ></Button>
</Grid>

Then in the code behind is very simple foreach by going through the Grid's children

foreach (Button b in this.myGrid.Children)
{
    // do whatever you want with b
    // b.Content = "I'm a button";
}

You can also .Tag each button which a specific identifier and even loop them as well

foreach (Button b in this.myGrid.Children)
{
    if(b.Tag == "main_buttons")
    {
        // do whatever you want with b when b.Tag is what you want
    }
}

enter image description here

Chubosaurus Software
  • 8,133
  • 2
  • 20
  • 26
  • Thank you! That's what I was looking for. Before reading this I deleted every button and then made _buttons[,], new Button constructor, etc... But your version is easier and I'll change my version into your one. Thanks! – Szymon Nov 30 '14 at 21:48
0

You can easily do this by giving each button an x:Name attribute which will make it available in the code-behind. In your constructor, collect all the buttons by name into a List. Then, you can just you standard foreach loops over the list.

Andrew
  • 1,482
  • 9
  • 16
  • That's what I wanted to avoid. – Szymon Nov 30 '14 at 21:43
  • Understandable - I agree with Chubosaurus' answer above in regards to learning to properly implement MVVM. I can't think of a good reason why you would need to iterate over the buttons of a control in the code-behind if MVVM is done properly. – Andrew Nov 30 '14 at 21:54
0

You can find the buttons from the visual tree:

search 'WPF FindVisualChildren' or have a look of this thread: Find all controls in WPF Window by type

Community
  • 1
  • 1
Eben
  • 554
  • 3
  • 11