Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I made a game , a mini version of battleship in wpf. I made 25 buttons instead of a 5x5 grid, now I want to change the properties of the buttons. Currently I am changing them by creating a click_down event for each button which is a time consuming method, can I change the properties using loop or a single function which I can call every time. The buttons are named as b1,b2.b3... b25. Is this possible?
Posted

1 solution

I want to say "no, it's impossible", but it cannot be serious, right? :-)

I have no idea how you created the array of buttons, but it's pretty obvious that you need an array of them. I would suggest you don't use XAML for that, because XAML would create individual buttons, not the array. However, I would suggest you create an instance of Grid in XAML and fill it in code behind. Doing it in XAML means to much of dumb manual work which is hard to maintain.
C#
const int matrixSize = 5;
Grid buttonGrid = // can come from XAML
Button[,] buttons = new Button[matrixSize, matrixSize];
for (int yIndex = 0; yIndex < matrixSize; ++yIndex)
    for (int xIndex = 0; xIndex < matrixSize; ++xIndex) {
        buttons[yIndex, xIndex] = new Button(); 
        buttonGrid.SetRow(buttons[yIndex, xIndex], yIndex);
        buttinGrid.SetColumn(buttons[yIndex, xIndex], xIndex);
        // set any button's properties
} //x, y loops
Using Grid.SetRow and Grid.SetColumn methods is the key; this is the way to set an attached property to an instance of a UIElement:
https://msdn.microsoft.com/en-us/library/ms589026%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.controls.grid_properties%28v=vs.110%29.aspx[^].

See also:
https://msdn.microsoft.com/en-us/library/vstudio/ms749011%28v=vs.100%29.aspx[^],
https://msdn.microsoft.com/en-us/library/vstudio/ms752914%28v=vs.100%29.aspx[^].

If you do it this way, you can use the similar loop any time later to change any other properties.

—SA
 
Share this answer
 
v4
Comments
Maciej Los 10-Aug-15 4:14am    
5ed!
BTW:
...XAML and feel it in code behind shouldn't be: ...XAML and fill it in code behind?
Sergey Alexandrovich Kryukov 10-Aug-15 10:04am    
Thank you, Maciej.
—SA
Member 11144482 10-Aug-15 14:17pm    
Well as I am new to wpf so just trying some things, this method works great but I would prefer XAML as I want to arrange the buttons as 5x5 grid. And I found some thing interesting online, its called UIHelper Class, turns out I can find any tool from the grid if and using a loop If I give them proper names,
Cheers,
Sergey Alexandrovich Kryukov 10-Aug-15 14:28pm    
Don't think at what you would prefer just yet, first learn things. The logic is simple: you would need to address your 5x5 elements by index anyway, myButtons[row, col], otherwise you don't have much to work with. Let's say you added them all in XAML? 25 elements? Dirty, bulky. But let's say you don't care. How would you access them? Yes you can. One way is: you could give them 25 individual names and write 25 assignment in code behind, such as
myButtons[0, 0] = button00;
myButtons[0, 1] = button01;
//...
Don't you see how could it be boring and hard to support? At the same time, I gave you complete code. In XAML, you only define the grid. You can use some other methods to defines the layout, but the idea is simple: if you have to work with array, you have to create an array.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900