WPF Button with Context Menu

Viewed 14874

I am new to WPF and am trying to bind a Context Menu to a Button with the Context Menu items coming from a View Model.

This is what I am doing:

<Button x:Name="btn" Content="Context Menu">
  <Button.ContextMenu>
    <ContextMenu x:Name="cm" ItemsSource="ItemsList"/>
  </Button.ContextMenu>
</Button>

private List<string> itemsList = null;
public List<string> ItemsList
{
  get
  {
    if(itemsList == null)
      itemsList = new List<string>(myStringArrayOfItems);
    return itemsList;                
  }
}

The XAML Editor keeps showing the error: The TypeConverter for "IEnumerable" does not support converting from a string.

What am I doing wrong here?

Also, assuming I get this working, what do I do to bind these items to a command and do some work when the item is clicked? I want to run the same command for all the menu items, just using the item string as a parameter.

2 Answers
Related