Click here to Skip to main content
13,901,646 members
Rate this:
Please Sign up or sign in to vote.
See more:
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?
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
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.
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?
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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[^].

Button AddButton(string caption, int row, int column, Grid parent) {
    Button button = new Button();
    button.Content = caption;
    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.

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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web06 | 2.8.190306.1 | Last Updated 26 Oct 2011
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100