locked
Grid of buttons

    Question

  • I want to build a 6x6 grid of buttons. And each button should be linked to common event handler.

    It should have a single event handling function.

    Although i am developing android apps from some time,i build this using the android.widget.gridview. But I am pretty new to C#. Please share the xaml and .cs file.

    Tuesday, July 29, 2014 10:02 AM

Answers

  • you can use a grid:

    <Grid>
    <Grid.ColumnDefinitions>
    <ColumnDefinition />
    <ColumnDefinition />
    <ColumnDefinition />
    <ColumnDefinition />
    <ColumnDefinition />
    <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinition>
    <RowDefinition />
    <RowDefinition />
    <RowDefinition />
    <RowDefinition />
    <RowDefinition />
    <RowDefinition />
    </Grid.RowDefinition>
    
    <Button Grid.Row="0" Grid.Column="0" />
    <Button Grid.Row="0" Grid.Column="1" />
    <Button Grid.Row="0" Grid.Column="2" />
    <Button Grid.Row="0" Grid.Column="3" />
    <Button Grid.Row="0" Grid.Column="4" />
    <Button Grid.Row="0" Grid.Column="5" />
    
    
    <Button Grid.Row="1" Grid.Column="0" />
    <Button Grid.Row="1" Grid.Column="1" />
    <Button Grid.Row="1" Grid.Column="2" />
    <Button Grid.Row="1" Grid.Column="3" />
    <Button Grid.Row="1" Grid.Column="4" />
    <Button Grid.Row="1" Grid.Column="5" />
    
    ...
    
    </Grid>
    

    and give easy button a event handler


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Tuesday, July 29, 2014 12:00 PM
  • Hi ashishshah1512,

    Or you could do with the help of the Loop.

    XAML:

            <GridView
                    x:Name="myGridView"
    	        IsItemClickEnabled="True"                            
    	        SelectionMode="None"
    	        HorizontalContentAlignment="Center"
    	        VerticalContentAlignment="Top" Margin="110,77,787,463"
    	        >
                <GridView.ItemsPanel>
                    <ItemsPanelTemplate>
                        <VariableSizedWrapGrid
    				Orientation="Horizontal"
    				MaximumRowsOrColumns="6"
    				HorizontalAlignment="Center"
    				VerticalAlignment="Top"
    				/>
                    </ItemsPanelTemplate>
                </GridView.ItemsPanel>
            </GridView>


    CS:

            public MainPage()
            {
                this.InitializeComponent();
                
                for (int i = 0; i < 6; i++)
                {
                    for (int j = 0; j < 6; j++)
                    {
                        Button myButton = new Button();
                        myButton.Click += myButton_Click;
                        myButton.Content = "Row: " + i + ",Column:" + j;
                        myGridView.Items.Add(myButton);
                    }
                }
    
            }
    
            void myButton_Click(object sender, RoutedEventArgs e)
            {
                throw new NotImplementedException();
            }

    Result:

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Tuesday, August 5, 2014 8:20 AM
    Moderator

All replies

  • you can use a grid:

    <Grid>
    <Grid.ColumnDefinitions>
    <ColumnDefinition />
    <ColumnDefinition />
    <ColumnDefinition />
    <ColumnDefinition />
    <ColumnDefinition />
    <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinition>
    <RowDefinition />
    <RowDefinition />
    <RowDefinition />
    <RowDefinition />
    <RowDefinition />
    <RowDefinition />
    </Grid.RowDefinition>
    
    <Button Grid.Row="0" Grid.Column="0" />
    <Button Grid.Row="0" Grid.Column="1" />
    <Button Grid.Row="0" Grid.Column="2" />
    <Button Grid.Row="0" Grid.Column="3" />
    <Button Grid.Row="0" Grid.Column="4" />
    <Button Grid.Row="0" Grid.Column="5" />
    
    
    <Button Grid.Row="1" Grid.Column="0" />
    <Button Grid.Row="1" Grid.Column="1" />
    <Button Grid.Row="1" Grid.Column="2" />
    <Button Grid.Row="1" Grid.Column="3" />
    <Button Grid.Row="1" Grid.Column="4" />
    <Button Grid.Row="1" Grid.Column="5" />
    
    ...
    
    </Grid>
    

    and give easy button a event handler


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Tuesday, July 29, 2014 12:00 PM
  • Hi ashishshah1512,

    Or you could do with the help of the Loop.

    XAML:

            <GridView
                    x:Name="myGridView"
    	        IsItemClickEnabled="True"                            
    	        SelectionMode="None"
    	        HorizontalContentAlignment="Center"
    	        VerticalContentAlignment="Top" Margin="110,77,787,463"
    	        >
                <GridView.ItemsPanel>
                    <ItemsPanelTemplate>
                        <VariableSizedWrapGrid
    				Orientation="Horizontal"
    				MaximumRowsOrColumns="6"
    				HorizontalAlignment="Center"
    				VerticalAlignment="Top"
    				/>
                    </ItemsPanelTemplate>
                </GridView.ItemsPanel>
            </GridView>


    CS:

            public MainPage()
            {
                this.InitializeComponent();
                
                for (int i = 0; i < 6; i++)
                {
                    for (int j = 0; j < 6; j++)
                    {
                        Button myButton = new Button();
                        myButton.Click += myButton_Click;
                        myButton.Content = "Row: " + i + ",Column:" + j;
                        myGridView.Items.Add(myButton);
                    }
                }
    
            }
    
            void myButton_Click(object sender, RoutedEventArgs e)
            {
                throw new NotImplementedException();
            }

    Result:

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Tuesday, August 5, 2014 8:20 AM
    Moderator