I am making a small battle game as a Windows Store app and I am using C# and XAML.
The main player has an inventory list that fills with strings dropped by the enemy. The items in the inventory can be chosen by the player when they click a button that causes a popup menu to appear.
The problem I am having is that I can not figure out how to dynamically populate the menu with the new item the enemy drops. Is there a way to databind the popup menu to the players inventory list?
Here's an example of a databound popup menu on a ListView
. I don't know what the code looks like for your enemy, but you should be able to generate a PopupMenu
using something similar to this
XAML
<ListBox>
<ListBoxItem Tag="chalk,cheese,beets" RightTapped="ListBoxItem_RightTapped">player one</ListBoxItem>
<ListBoxItem Tag="vodka,ak ammo" RightTapped="ListBoxItem_RightTapped">player two</ListBoxItem>
</ListBox>
C#
private async void ListBoxItem_RightTapped(object sender, RightTappedRoutedEventArgs e)
{
PopupMenu menu = new PopupMenu();
string[] items = ((ListBoxItem)sender).Tag.ToString().Split(',');
foreach (string item in items)
{
menu.Commands.Add(new UICommand(item, (command) =>
{
// do stuff
}));
}
var chosenCommand = await menu.ShowAsync(e.GetPosition(this));
// do something with chosen command value
}