Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: XAML VB.NET
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 24-Oct-11 11:27am
Rate this: bad
good
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.
  Permalink  
Comments
SAKryukov at 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
Nishant Sivakumar at 25-Oct-11 7:45am
   
Thanks SA.
annatawa at 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 at 25-Oct-11 15:31pm
   
Sorry for lame question, how can we make Listbox use Dockpanel?
Rate this: bad
good
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 http://msdn.microsoft.com/en-us/library/ms752914.aspx[^].
 
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
  Permalink  
v2
Comments
Nishant Sivakumar at 25-Oct-11 7:46am
   
Good alternate solution (what the OP originally wanted). Got my 5!
SAKryukov at 25-Oct-11 10:47am
   
Thank you, Nishant.
--SA

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

  Print Answers RSS
0 Kornfeld Eliyahu Peter 275
1 Maciej Los 214
2 Sergey Alexandrovich Kryukov 200
3 King Fisher 185
4 OriginalGriff 163
0 OriginalGriff 6,223
1 DamithSL 4,724
2 Maciej Los 4,196
3 Kornfeld Eliyahu Peter 3,894
4 Sergey Alexandrovich Kryukov 3,496


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 26 Oct 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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