I have this xaml code
<StackPanel Margin="0 50 0 0">
<Button Style="{StaticResource WordButton}" Content="Гитара"
x:Name="FirstWordButton" Click="FirstWordButton_Click"/>
<Button Style="{StaticResource WordButton}" Content="Дерево"
x:Name="SecondWordButton" Click="SecondWordButton_Click"/>
<Button Style="{StaticResource WordButton}" Content="Дверь"
x:Name="ThirdWordButton" Click="ThirdWordButton_Click"/>
<Button Style="{StaticResource WordButton}" Content="Лес"
x:Name="FourthWordButton" Click="FourthWordButton_Click"/>
</StackPanel>
I`m not very happy about the fact that for handle, for instance all buttons, I need to write their names. Is it possible to create a list or resource and put all there all these buttons to handle buttons like this?
foreach (Button B in "resource")
................
Sorry for bad English!
The Button
elements are added to the Children
collection of the StackPanel
when the XAML markup is processed so you could just give the StackPanel
a name in the XAML markup:
<StackPanel x:Name="sp" ... />
...and then access the Button
elements like this in the code:
foreach (Button button in sp.Children.OfType<Button>())