Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
163 views
in Technique[技术] by (71.8m points)

c# - How to create dynamic row and columns

I need to create dynamic row and columns base on the parameter, like in my json I will have x and y which will be no. of rows and columns respectively, and there will be content with x and y which I have to put them in their respective row and column, I tried to use below code but found that uniform grid is not available in UWP App. Also thought about DataTemplateSelector, but am not sure if that is a good idea to use it.

Xaml:

 <ItemsControl ItemsSource="{Binding MyCollection}">

            <!-- This panel will be used to hold the items -->
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <un Rows="8"
                                 Columns="8" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>

            <!-- Each item will be drawn using this template -->
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <ContentControl Content="{Binding }"
                            Style="{StaticResource MyButtonStyle}" />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

Please anybody can guide me what approach should I follow.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You don't need xaml to get a uniform grid. See solution below

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Buttons
{
    public partial class Form1 : Form
    {
        const int ROWS = 5;
        const int COLS = 10;

        public Form1()
        {
            InitializeComponent();
            this.Load += new System.EventHandler(this.Form1_Load);
        }
        public void Form1_Load(object sender, EventArgs e)
        {
            new MyButton(ROWS, COLS, this);
        }


    }
    public class MyButton : Button
    {
        const int WIDTH = 50;
        const int HEIGHT = 50;
        const int SPACE = 5;
        const int BORDER = 20;

        public static List<List<MyButton>> buttons { get; set; }
        public static List<MyButton> buttonList { get; set; }
        public Form1 form1;
        public int row { get; set; }
        public int col { get; set; }
        public MyButton()
        {
        }
        public MyButton(int rows, int cols, Form1 form1)
        {
            buttons = new List<List<MyButton>>();
            buttonList = new List<MyButton>();

            this.form1 = form1;
            for (int row = 0; row < rows; row++)
            {
                List<MyButton> newRow = new List<MyButton>();
                buttons.Add(newRow);
                for (int col = 0; col < cols; col++)
                {
                    MyButton newButton = new MyButton();
                    newButton.Height = HEIGHT;
                    newButton.Width = WIDTH;
                    newButton.Top = row * (HEIGHT + SPACE) + BORDER;
                    newButton.Left = col * (WIDTH + SPACE) + BORDER;
                    newButton.row = row;
                    newButton.col = col;
                    newRow.Add(newButton);
                    buttonList.Add(newButton);
                    newButton.Click += new System.EventHandler(Button_Click);
                    form1.Controls.Add(newButton);
                }
            }
        }
        public void Button_Click(object sender, EventArgs e)
        {
            MyButton button = sender as MyButton;
            MessageBox.Show(string.Format("Pressed Button Row {0} Column {1}", button.row, button.col));

        }

    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...