【问题标题】:How to create dynamic row and columns如何创建动态行和列
【发布时间】:2016-05-22 10:42:34
【问题描述】:

我需要根据参数创建动态行和列,就像在我的 json 中一样,我将有 x 和 y,这将是没有的。分别是行和列,并且会有 x 和 y 的内容,我必须将它们放在各自的行和列中,我尝试使用下面的代码,但发现 UWP 应用程序中没有统一网格。也考虑过 DataTemplateSelector,但不确定使用它是否是个好主意。

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>

请任何人指导我应该遵循什么方法。

【问题讨论】:

  • 虽然UWP没有内置UniformGrid,但是我们可以自己实现或者使用第三方的UniformGrid,比如WinRTXamlToolkit。可以参考我的previous answer,可能会有帮助。

标签: c# xaml uwp


【解决方案1】:

您不需要 xaml 来获得统一的网格。请参阅下面的解决方案

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));

        }

    }
}

【讨论】:

  • 谢谢,但是我需要它首先为 x 行和 y 列创建一个网格,然后我需要将控件放置在特定的行和列中,例如。在 json 中,我得到了 4 行和 4 列,而不是网格应该是 4*4,并且会有第 2 行和第 1 列的内容,因此需要将该内容放在该网格的第 2 行和第 1 列中。
  • 这就是为什么我有: List> 按钮;和 List buttonList;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-14
  • 2013-05-06
  • 2017-09-18
  • 2012-04-06
  • 2013-11-18
  • 2019-09-04
相关资源
最近更新 更多