简体   繁体   English

如何在窗口中添加多个按钮? C#

[英]How to add multiple buttons to a window? C#

I am currently writing a game the requires a shop. 我目前正在编写需要商店的游戏。 I am planing in making 1 button open the shop/show all the buttons of things you can buy. 我打算开1个按钮来打开商店/显示所有可以购买的按钮。 I am currently having a hard time adding the button to the actual window. 我目前很难将按钮添加到实际窗口中。 the "contorls" function isn't showing up as a option. “控制”功能未显示为选项。

for (int i = 0; i < weaponsList.Count; i++)
        {
            Button newButton = new Button();
            newButton.Name = Convert.ToString(i);
            newButton.Content = weaponsList[i].WeaponName += weaponsList[i].MinDamage += weaponsList[i].MaxDamage += weaponsList[i].Cost;
            newButton.Visibility = Visibility.Visible;
            newButton.Width = 502;
            newButton.Height = 78;
        }

The for loop is so that it will keep adding buttons for the number of weapons. for循环是这样,它将继续为武器数量添加按钮。 i have tried doing the controls thing but it hasn't worked. 我试图做控件的事情,但没有成功。

You put all buttons in same position. 您将所有按钮放在同一位置。 If you want see all buttons, you must have different positions. 如果要查看所有按钮,则必须具有不同的位置。 Quick good solution is here: 快速好的解决方案在这里:

for (int i = 0; i < weaponsList.Count; i++)
{
  Button newButton = new Button();
  newButton.Name = Convert.ToString(i);
  newButton.Content = weaponsList[i].WeaponName += weaponsList[i].MinDamage += weaponsList[i].MaxDamage += weaponsList[i].Cost;
  newButton.Visibility = Visibility.Visible;
  newButton.Width = 502+10*i;
  newButton.Height = 78+10*i;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM