简体   繁体   中英

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. 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. 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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