Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Greetings!
I need to create a grid that holds 100 buttons. Button names should be btn_0_0, btn_0_1 ect or similar.
How to add a dynamic button creation, so it would create the buttons under sequence and place all those inside grid?
Searched a lot, but didnt find an example that will work in WPF (VB).
Googled & searched codeproject too, found some information and were able to make it in WinForms. The thing is that I must use WPF, and things are different there. Can anyone help please?
Posted

First, please understand that all controls are added "dynamically". XAML only helps to automate creation of the window setup, but behind the scene all is done dynamically, during run time.

The main problem here is to setup row and column of the button in the grid. The unusual feature which makes WPF different from other UI libraries is using dependency properties, see http://msdn.microsoft.com/en-us/library/ms752914.aspx[^].

C#
Button AddButton(string caption, int row, int column, Grid parent) {
    Button button = new Button();
    button.Content = caption;
    parent.Children.Add(button);
    Grid.SetRow(button, row);
    Grid.SetColumn(button, column);
    return button;
}


Just call a method like this in a double cycle (by rows and columns).

I should also note that having so many buttons is a sign of the problem of your design. You can use a variant suggested by Nishant. There can be different designs, based on TreeView which nodes define required action, so the can be structured hierarchically.

—SA
 
Share this answer
 
v2
Comments
Nish Nishant 25-Oct-11 7:46am    
Good alternate solution (what the OP originally wanted). Got my 5!
Sergey Alexandrovich Kryukov 25-Oct-11 10:47am    
Thank you, Nishant.
--SA
A templated ListBox that uses a DockPanel to display items that are rendered as buttons (through an appropriate ItemTemplate) may be a far better design. Read up on MSDN or here on Code Project on how you can use ListBox templates.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 24-Oct-11 17:37pm    
I agree with you: the design really need an alternative solutions, my 5. I credited your advice in my solution.

Nevertheless, I explained how to solve the problem as OP wanted -- this is mostly about using dependency properties, please see my solution.
--SA
Nish Nishant 25-Oct-11 7:45am    
Thanks SA.
annatawa 25-Oct-11 15:24pm    
Most possibly you are right. The thing is we are complete beginners, doing a project that is much more that we know (as college task).
Its sort of Civilization game that has a 10X10 map (every one represented with button so far). I will definitely try your suggestion.
Thanks a lot
annatawa 25-Oct-11 15:31pm    
Sorry for lame question, how can we make Listbox use Dockpanel?

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