0

如何创建一个圆形按钮并以编程方式设置它的位置?我正在尝试创建一组按钮并设置它们的位置,但是为什么代码没有以正确的方式工作。所有按钮都出现在 1 个位置,它们不是圆圈。

    private void addButtons(int hLimit, int vLimit)
    {
        dotButton[] dotsArray = new dotButton[hLimit * vLimit];

        int scale = 30;
        int yStart = 100;
        int xStart = 50;
        int yEnd = yStart + (hLimit - 1)*scale;
        int xEnd = xStart + (vLimit - 1)*scale;


        for (int i = 0; i < hLimit; ++i)
        {
            for (int j = 0; j < vLimit; ++j)
            {
                int id = j*hLimit + i;
                dotsArray[id] = new dotButton(id, j, i);
                dotsArray[id].Width = Width = 10;
                dotsArray[id].Height = Height = 10;
                dotsArray[id].Style = Resources["Sircle Button"] as Style;
                dotsArray[id].Click += new RoutedEventHandler(dotClick);
                double x = xStart + j * scale - Width/2;
                double y = yStart + i * scale - Height/2;
                dotsArray[id].Margin = new Thickness(x, y, x + Width, y + Height);

                myGrid.Children.Add(dotsArray[id]);
            }
        }
    }

dotButton 是我编写的按钮类

    public class dotButton : Button
{
    private int id;
    private int x;
    private int y;

    public int ID
    {
        get { return id; }
        set { id = value; }
    }
    public int X
    {
        get { return x; }
        set { x = value; }
    }
    public int Y
    {
        get { return y; }
        set { y = value; }
    }

    public dotButton(int ID, int X, int Y)
    {
        this.ID = ID;
        this.X = X;
        this.Y = Y;
    }
}

我究竟做错了什么?

此外,当应用程序启动时,它看起来像这样。为什么会这样?

在此处输入图像描述

4

0 回答 0